PHP - Display Updated time


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function time_ago( $datefrom )
  2. {
  3. $dateto = date('Y-m-d H:i:s');
  4. // echo $dateto."-";
  5. // echo $datefrom;
  6.  
  7. if($datefrom<=0) { return "A long time ago"; }
  8. if($dateto==-1) { $dateto = time(); }
  9.  
  10. $difference = $dateto - $datefrom;
  11.  
  12. // Seconds
  13. if($difference < 60)
  14. {
  15. // $time_ago = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' ago';
  16. $time_ago ='Just now';
  17. }
  18.  
  19. // Minutes
  20. else if( $difference < 60*60 )
  21. {
  22. $ago_seconds = $difference % 60;
  23. $ago_seconds = ( ( $ago_seconds AND $ago_seconds > 1 ) ? ' and '.$ago_seconds.' seconds' : ( $ago_seconds == 1 ? ' and '.$ago_seconds.' second' : '' ) );
  24. $ago_minutes = floor( $difference / 60 );
  25. $ago_minutes = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' ).' ago';
  26. $time_ago = $ago_minutes;
  27. }
  28.  
  29. // Hours
  30. else if ( $difference < 60*60*24 )
  31. {
  32. $ago_minutes = round( $difference / 60 ) % 60 ;
  33. $ago_minutes = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' ));
  34. $ago_hours = floor( $difference / ( 60 * 60 ) );
  35. $ago_hours = $ago_hours . ' hour'. ( $ago_hours > 1 ? 's' : '' );
  36. $time_ago = $ago_hours.$ago_minutes.' ago';
  37. }
  38.  
  39. // Days
  40. else if ( $difference < 60*60*24*7 )
  41. {
  42. $ago_hours = round( $difference / 3600 ) % 24 ;
  43. $ago_hours = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' ));
  44. $ago_days = floor( $difference / ( 3600 * 24 ) );
  45. $ago_days = $ago_days . ' day' . ($ago_days > 1 ? 's' : '' );
  46. $time_ago = $ago_days.$ago_hours.' ago';
  47. }
  48.  
  49. // Weeks
  50. else if ( $difference < 60*60*24*30 )
  51. {
  52. $ago_days = round( $difference / ( 3600 * 24 ) ) % 7;
  53. $ago_days = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' ));
  54. $ago_weeks = floor( $difference / ( 3600 * 24 * 7) );
  55. $ago_weeks = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' );
  56. $time_ago = $ago_weeks.$ago_days.' ago';
  57. }
  58.  
  59. // Months
  60. else if ( $difference < 60*60*24*365 )
  61. {
  62. $days_diff = round( $difference / ( 60 * 60 * 24 ) );
  63. $ago_days = $days_diff % 30 ;
  64. $ago_weeks = round( $ago_days / 7 ) ;
  65. $ago_weeks = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) );
  66. $ago_months = floor( $days_diff / 30 );
  67. $ago_months = $ago_months .' month'. ( $ago_months > 1 ? 's' : '' );
  68. $time_ago = $ago_months.$ago_weeks.' ago';
  69. }
  70.  
  71. // Years
  72. else if ( $difference >= 60*60*24*365 )
  73. {
  74. $ago_months = round( $difference / ( 60 * 60 * 24 * 30.5 ) ) % 12;
  75. $ago_months = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) );
  76. $ago_years = floor( $difference / ( 60 * 60 * 24 * 365 ) );#30 * 12
  77. $ago_years = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ;
  78. $time_ago = $ago_years.$ago_months.' ago';
  79. }
  80.  
  81. return $time_ago;
  82. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.