PHP displays time as 'just', 'n minutes/hours ago', etc.

WBOY
Release: 2016-07-25 08:43:12
Original
1052 people have browsed it
In many occasions, in order to show the timeliness of information, the time is usually displayed as "just now", "5 minutes ago", "3 hours ago", etc. instead of printing the time directly. For example, Weibo and SNS applications have long used this function. The time format generally stored in the database is Unix timestamp, so here is a PHP function that converts Unix timestamp into timeline display.
  1. date_default_timezone_set('PRC');
  2. $date = "1351836000";
  3. echo tranTime($date);
  4. function transfer_time($time)
  5. {
  6. $rtime = date(" m-d H:i",$time);
  7. $htime = date("H:i",$time);
  8. $time = time() - $time;
  9. if ($time < 60)
  10. {
  11. $str = 'just';
  12. }
  13. elseif ($time < 60 * 60)
  14. {
  15. $min = floor($time/60);
  16. $str = $min.'minutes ago';
  17. }
  18. elseif ($time < 60 * 60 * 24)
  19. {
  20. $h = floor($time/(60*60));
  21. $str = $h.'hours ago'.$htime;
  22. }
  23. elseif ( $time < 60 * 60 * 24 * 3)
  24. {
  25. $d = floor($time/(60*60*24));
  26. if($d==1)
  27. $str = 'yesterday'.$ rtime;
  28. else
  29. $str = 'the day before yesterday'.$rtime;
  30. }
  31. else
  32. {
  33. $str = $rtime;
  34. }
  35. return $str;
  36. }
  37. ?>
Copy code

PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template