PHP processing date format is seconds ago, minutes ago, hours ago, yesterday, the day before

Source: Internet
Author: User

Source: http://www.crazyphper.com


  1. /**
  2. * Change the date format to a different display style according to the following rules
  3. * Less than 1 minutes shows how many seconds ago
  4. * Less than 1 hours, show how many minutes ago
  5. * Within a day, show how many hours ago
  6. * Within 3 days, show the day before yesterday 22:23 or yesterday: 12:23.
  7. * More than 3 days, the full date is displayed.
  8. * @static
  9. * @param $sorce _date data Source Date Unix Timestamp
  10. * @return void
  11. */
  12. public static function Getdatestyle ($sorce _date) {
  13. Self:: $nowTime = time (); Get today time stamp
  14. Echo ' data source timestamp: '. $sorce _date. ' = '. Date (' y-m-d h:i:s ', $sorce _date);
  15. echo "\ nthe 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. Hours
  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. Days
  30. Case ($sorce _date+3600*24) >=self:: $nowTime:
  31. $temp _time = date (' h ', Self:: $nowTime)-date (' H ', $sorce _date);
  32. $timeHtml = $temp _time. ' Hours ago ';
  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. Before
  40. Case ($sorce _date+3600*24*3) >=self:: $nowTime:
  41. $temp _time = Date (' h:i ', $sorce _date);
  42. $timeHtml = ' The day before yesterday '. $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. }
Copy Code
  • 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.