Advertisement
Guest User

Codeigniter time ago function

a guest
Sep 9th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1.   function time_ago($ptime) {
  2.     $today = time();
  3.     $createdday = mysql_to_unix($ptime);
  4.     $datediff = abs($today - $createdday);
  5.     $difftext = "";
  6.     $years = floor($datediff / (365 * 60 * 60 * 24));
  7.     $months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
  8.     $days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
  9.     $hours = floor($datediff / 3600);
  10.     $minutes = floor($datediff / 60);
  11.     $seconds = floor($datediff);
  12.     //year checker
  13.     if ($difftext == "") {
  14.       if ($years > 1)
  15.       $difftext = $years . " years ago";
  16.       elseif ($years == 1)
  17.       $difftext = $years . " year ago";
  18.     }
  19.     //month checker
  20.     if ($difftext == "") {
  21.       if ($months > 1)
  22.       $difftext = $months . " months ago";
  23.       elseif ($months == 1)
  24.       $difftext = $months . " month ago";
  25.     }
  26.     //month checker
  27.     if ($difftext == "") {
  28.       if ($days > 1)
  29.       $difftext = $days . " days ago";
  30.       elseif ($days == 1)
  31.       $difftext = $days . " day ago";
  32.     }
  33.     //hour checker
  34.     if ($difftext == "") {
  35.       if ($hours > 1)
  36.       $difftext = $hours . " hours ago";
  37.       elseif ($hours == 1)
  38.       $difftext = $hours . " hour ago";
  39.     }
  40.     //minutes checker
  41.     if ($difftext == "") {
  42.       if ($minutes > 1)
  43.       $difftext = $minutes . " minutes ago";
  44.       elseif ($minutes == 1)
  45.       $difftext = $minutes . " minute ago";
  46.     }
  47.     //seconds checker
  48.     if ($difftext == "") {
  49.       if ($seconds > 1)
  50.       $difftext = $seconds . " seconds ago";
  51.       elseif ($seconds == 1)
  52.       $difftext = $seconds . " second ago";
  53.     }
  54.     return $difftext;
  55.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement