The php processing date format is seconds ago, minutes ago, hours ago, yesterday, and the day before yesterday.

Source: Internet
Author: User
The php processing date format is seconds ago, minutes ago, hours ago, yesterday, and the day before yesterday.

Source: http://www.crazyphper.com


  1. /**
  2. * Change the date format to different display styles according to the following rules:
  3. * The number of seconds before the display time is less than 1 minute
  4. * The number of minutes before the display time is less than 1 hour
  5. * How many hours ago are displayed in a day?
  6. * Within three days, or the day before yesterday is displayed.
  7. * If it exceeds 3 days, the complete date is displayed.
  8. * @ Static
  9. * @ Param $ sorce_date date unix timestamp
  10. * @ Return void
  11. */
  12. Public static function getDateStyle ($ sorce_date ){
  13. Self: $ nowTime = time (); // get today's timestamp
  14. // Echo 'data source timestamp: '. $ sorce_date.' = '. date ('Y-m-d H: I: S', $ sorce_date );
  15. // Echo "\ n current timestamp:". date ('Y-m-d H: I: S', self: $ nowTime). "\ n ";
  16. $ TimeHtml = ''; // return text format
  17. $ Temp_time = 0;
  18. Switch ($ sorce_date ){
  19. // One minute
  20. Case ($ sorce_date + 60)> = self: $ nowTime:
  21. $ Temp_time = self: $ nowTime-$ sorce_date;
  22. $ TimeHtml = $ temp_time. "seconds ago ";
  23. Break;
  24. // Hour
  25. Case ($ sorce_date + 3600)> = self: $ nowTime:
  26. $ Temp_time = date ('I', self: $ nowTime-$ sorce_date );
  27. $ TimeHtml = $ temp_time. "minutes ago ";
  28. Break;
  29. // Day
  30. Case ($ sorce_date + 3600*24)> = self: $ nowTime:
  31. $ Temp_time = date ('H', self: $ nowTime)-date ('H', $ sorce_date );
  32. $ TimeHtml = $ temp_time. 'hour before ';
  33. Break;
  34. // Yesterday
  35. Case ($ sorce_date + 3600*24*2)> = self: $ nowTime:
  36. $ Temp_time = date ('H: I ', $ sorce_date );
  37. $ TimeHtml = 'Yesterday '. $ temp_time;
  38. Break;
  39. // The day before yesterday
  40. Case ($ sorce_date + 3600*24*3)> = self: $ nowTime:
  41. $ Temp_time = date ('H: I ', $ sorce_date );
  42. $ TimeHtml = 'day before Day'. $ temp_time;
  43. Break;
  44. // 3 days ago
  45. Case ($ sorce_date + 3600*24*4)> = self: $ nowTime:
  46. $ TimeHtml = '3 days ago ';
  47. Break;
  48. Default:
  49. $ TimeHtml = date ('Y-m-D', $ sorce_date );
  50. Break;
  51. }
  52. Return $ timeHtml;
  53. }

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.