Home > Backend Development > PHP Tutorial > PHP implements timeline function (just now, 5 minutes ago)

PHP implements timeline function (just now, 5 minutes ago)

WBOY
Release: 2016-07-25 08:55:33
Original
1619 people have browsed it
  1. function tranTime($time) {
  2. $rtime = date("m-d H:i",$time);
  3. $htime = date("H:i",$time);
  4. $time = time() - $time;
  5. if ($time < 60) {
  6. $str = 'just';
  7. }
  8. elseif ($time < 60 * 60) {
  9. $min = floor ($time/60);
  10. $str = $min.'minutes ago';
  11. } //Timeline bbs.it-home.org
  12. elseif ($time < 60 * 60 * 24) {
  13. $h = floor($time/(60*60));
  14. $str = $h.'hour ago'.$htime;
  15. }
  16. elseif ($time < 60 * 60 * 24 * 3) {
  17. $d = floor ($time/(60*60*24));
  18. if($d==1)
  19. $str = 'yesterday'.$rtime;
  20. else
  21. $str = 'the day before yesterday'.$rtime;
  22. }
  23. else {
  24. $str = $rtime;
  25. }
  26. return $str;
  27. }
Copy code

The parameter $time in function tranTime() must be a Unix timestamp. If not, please use strtotime() first Convert it to Unix timestamp.

Call example:

  1. //php timeline function example
  2. $times="1286861696 ";
  3. echo tranTime($times);
Copy code

>>> You may be interested article: Sample code for php to get time and how many minutes ago Small example of PHP deleting all files created N minutes ago PHP displays the implementation code just now, a few minutes ago, and a few hours ago according to the time php implements the function code that posted the information a few minutes ago



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