PHP Displays the time as "just" "N Minutes/hour ago", etc.

Source: Internet
Author: User
In many occasions in order to show the timeliness of information, the time is generally displayed as "just", "5 minutes Ago", "3 hours ago", and so on, rather than directly print out the time. such as microblogging, SNS applications for the longest use of this feature. The time format typically stored in the database is a UNIX timestamp, so here is a PHP function that converts the UNIX timestamp to the 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. ' Hour 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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.