Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.65 KB | None | 0 0
  1. add_filter('get_the_time', 'my_get_the_time', 100, 3 );
  2.  
  3. function my_get_the_time($the_time, $d, $post){
  4.     return findTimeAgo( get_post_time('U', true, $post) );
  5. }
  6.  
  7. function findTimeAgo( $past ) {
  8.    
  9.     // creates the "time ago" string. This always starts with an "about..."
  10.     $timeAgo = "";
  11.    
  12.     // finds the time difference
  13.     $timeDifference = time() - $past;
  14.    
  15.     // less than 29secs
  16.     if($timeDifference <= 29) {
  17.       $timeAgo = "less than a minute";
  18.     }
  19.     // more than 29secs and less than 1min29secss
  20.     else if($timeDifference > 29 && $timeDifference <= 89) {
  21.       $timeAgo = "1 minute";
  22.     }
  23.     // between 1min30secs and 44mins29secs
  24.     else if($timeDifference > 89 &&
  25.       $timeDifference <= ((MINUTE_IN_SECONDS * 44) + 29)
  26.     ) {
  27.       $minutes = floor($timeDifference / MINUTE_IN_SECONDS);
  28.       $timeAgo = $minutes." minutes";
  29.     }
  30.     // between 44mins30secs and 1hour29mins29secs
  31.     else if(
  32.       $timeDifference > ((MINUTE_IN_SECONDS * 44) + 29)
  33.       &&
  34.       $timeDifference < ((MINUTE_IN_SECONDS * 89) + 29)
  35.     ) {
  36.       $timeAgo = "about 1 hour";
  37.     }
  38.     // between 1hour29mins30secs and 23hours59mins29secs
  39.     else if(
  40.       $timeDifference > (
  41.         (MINUTE_IN_SECONDS * 89) +
  42.         29
  43.       )
  44.       &&
  45.       $timeDifference <= (
  46.         (HOUR_IN_SECONDS * 23) +
  47.         (MINUTE_IN_SECONDS * 59) +
  48.         29
  49.       )
  50.     ) {
  51.       $hours = floor($timeDifference / HOUR_IN_SECONDS);
  52.       $timeAgo = $hours." hours";
  53.     }
  54.     // between 23hours59mins30secs and 47hours59mins29secs
  55.     else if(
  56.       $timeDifference > (
  57.         (HOUR_IN_SECONDS * 23) +
  58.         (MINUTE_IN_SECONDS * 59) +
  59.         29
  60.       )
  61.       &&
  62.       $timeDifference <= (
  63.         (HOUR_IN_SECONDS * 47) +
  64.         (MINUTE_IN_SECONDS * 59) +
  65.         29
  66.       )
  67.     ) {
  68.       $timeAgo = "1 day";
  69.     }
  70.     // between 47hours59mins30secs and 29days23hours59mins29secs
  71.     else if(
  72.       $timeDifference > (
  73.         (HOUR_IN_SECONDS * 47) +
  74.         (MINUTE_IN_SECONDS * 59) +
  75.         29
  76.       )
  77.       &&
  78.       $timeDifference <= (
  79.         (DAY_IN_SECONDS * 29) +
  80.         (HOUR_IN_SECONDS * 23) +
  81.         (MINUTE_IN_SECONDS * 59) +
  82.         29
  83.       )
  84.     ) {
  85.       $days = floor($timeDifference / DAY_IN_SECONDS);
  86.       $timeAgo = $days." days";
  87.     }
  88.     // between 29days23hours59mins30secs and 59days23hours59mins29secs
  89.     else if(
  90.       $timeDifference > (
  91.         (DAY_IN_SECONDS * 29) +
  92.         (HOUR_IN_SECONDS * 23) +
  93.         (MINUTE_IN_SECONDS * 59) +
  94.         29
  95.       )
  96.       &&
  97.       $timeDifference <= (
  98.         (DAY_IN_SECONDS * 59) +
  99.         (HOUR_IN_SECONDS * 23) +
  100.         (MINUTE_IN_SECONDS * 59) +
  101.         29
  102.       )
  103.     ) {
  104.       $timeAgo = "about 1 month";
  105.     }
  106.     // between 59days23hours59mins30secs and 1year (minus 1sec)
  107.     else if(
  108.       $timeDifference > (
  109.         (DAY_IN_SECONDS * 59) +
  110.         (HOUR_IN_SECONDS * 23) +
  111.         (MINUTE_IN_SECONDS * 59) +
  112.         29
  113.       )
  114.       &&
  115.       $timeDifference < YEAR_IN_SECONDS
  116.     ) {
  117.       $months = round($timeDifference / MONTH_IN_SECONDS);
  118.       // if months is 1, then set it to 2, because we are "past" 1 month
  119.       if($months == 1) {
  120.         $months = 2;
  121.       }
  122.      
  123.       $timeAgo = $months." months";
  124.     }
  125.     // between 1year and 2years (minus 1sec)
  126.     else if(
  127.       $timeDifference >= YEAR_IN_SECONDS
  128.       &&
  129.       $timeDifference < (YEAR_IN_SECONDS * 2)
  130.     ) {
  131.       $timeAgo = "about 1 year";
  132.     }
  133.     // 2years or more
  134.     else {
  135.       $years = floor($timeDifference / YEAR_IN_SECONDS);
  136.       $timeAgo = "over ".$years." years";
  137.     }
  138.    
  139.     return $timeAgo." ago";
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement