Advertisement
reenadak

2018-10-14_time ago_function

Feb 20th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1.     /*
  2.     - Time Ago (Unix)
  3.     - Converts unix time into the human readable "x seconds/minutes/hours
  4.       /days/weeks/months/years ago/into the future" format
  5.    
  6.     $unixTime     - The time to turn into the "x time ago" format,
  7.                     can be generated with PHP's time() function which
  8.                     is the number of seconds that passed since Jan 1 1970.
  9.     */
  10.    
  11. function str_time_ago( $val )
  12. {
  13.     if($val==0) return 0; // solution for zero date
  14.     $diff = time() - strtotime($val) -(12.5*3600); // last figure is adhoc adjustment for timezone.
  15.  
  16.     if( $diff < 1 ) { return 'Just now or error'; }
  17.  
  18.     $condition = array(
  19.         365.25 * 24 * 60 * 60  =>  'year',
  20.         30 * 24 * 60 * 60      =>  'month',
  21.         7 * 24 * 60 * 60       =>  'week',
  22.         24 * 60 * 60           =>  'day',
  23.         60 * 60                =>  'hour',
  24.         60                     =>  'minute',
  25.         1                      =>  'second'
  26.     );
  27.  
  28.     foreach( $condition as $secs => $str )
  29.     {
  30.         $d = $diff / $secs;
  31.  
  32.         if( $d >= 1 )
  33.         {
  34.             $t = round( $d );
  35.             return $t . ' ' . $str . ( $t > 1 ? 's' : '' ) . ' ago';
  36.         }
  37.     }
  38. }
  39.  
  40. // Usage
  41. <td class="col-md-2"><?php echo str_time_ago($row['created_at']); ?> </td>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement