PHP shows the time as "just" "n minutes/hour ago"-php Tutorial

Source: Internet
Author: User
PHP displays the time as "just", "n minutes ago", etc.
In many cases, in order to display the timeliness of information, the time is usually displayed as "just", "5 minutes ago", "3 hours ago", and so on, instead of printing the time directly. For example, Weibo and SNS applications use this function for the longest time. Generally, the time format stored in the database is Unix timestamp, so here we record a PHP function that converts the Unix timestamp into a timeline display.

  1. Date_default_timezone_set ('prc ');
  2. USD 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 = 'gang ';
  12. }
  13. Elseif ($ time <60*60)
  14. {
  15. $ Min = floor ($ time/60 );
  16. $ Str = $ min. 'minute ago ';
  17. }
  18. Elseif ($ time <60*60*24)
  19. {
  20. $ H = floor ($ time/(60*60 ));
  21. $ Str = $ h. 'hour before '. $ 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 = 'day before Day'. $ rtime;
  30. }
  31. Else
  32. {
  33. $ Str = $ rtime;
  34. }
  35. Return $ str;
  36. }
  37. ?>


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.