Home > Backend Development > PHP Tutorial > PHP handles date formats as seconds ago, minutes ago, hours ago, yesterday, and the day before yesterday

PHP handles date formats as seconds ago, minutes ago, hours ago, yesterday, and the day before yesterday

WBOY
Release: 2016-07-25 09:09:02
Original
1155 people have browsed it

出处:http://www.crazyphper.com


  1. /**
  2. * 将日期格式根据以下规律修改为不同显示样式
  3. * 小于1分钟 则显示多少秒前
  4. * 小于1小时,显示多少分钟前
  5. * 一天内,显示多少小时前
  6. * 3天内,显示前天22:23或昨天:12:23。
  7. * 超过3天,则显示完整日期。
  8. * @static
  9. * @param $sorce_date 数据源日期 unix时间戳
  10. * @return void
  11. */
  12. public static function getDateStyle($sorce_date){
  13. self::$nowTime = time(); //获取今天时间戳
  14. // echo '数据源时间戳:'.$sorce_date . ' = '. date('Y-m-d H:i:s',$sorce_date);
  15. // echo "\n 当前时间戳:". date('Y-m-d H:i:s',self::$nowTime)."\n";
  16. $timeHtml = ''; //返回文字格式
  17. $temp_time = 0;
  18. switch($sorce_date){
  19. //一分钟
  20. case ($sorce_date+60)>=self::$nowTime:
  21. $temp_time = self::$nowTime-$sorce_date;
  22. $timeHtml = $temp_time ."秒前";
  23. break;
  24. //小时
  25. case ($sorce_date+3600)>=self::$nowTime:
  26. $temp_time = date('i',self::$nowTime-$sorce_date);
  27. $timeHtml = $temp_time ."分钟前";
  28. break;
  29. //天
  30. case ($sorce_date+3600*24)>=self::$nowTime:
  31. $temp_time = date('H',self::$nowTime)-date('H',$sorce_date);
  32. $timeHtml = $temp_time .'小时前';
  33. break;
  34. //昨天
  35. case ($sorce_date+3600*24*2)>=self::$nowTime:
  36. $temp_time = date('H:i',$sorce_date);
  37. $timeHtml = '昨天'.$temp_time ;
  38. break;
  39. //前天
  40. case ($sorce_date+3600*24*3)>=self::$nowTime:
  41. $temp_time = date('H:i',$sorce_date);
  42. $timeHtml = '前天'.$temp_time ;
  43. break;
  44. //3天前
  45. case ($sorce_date+3600*24*4)>=self::$nowTime:
  46. $timeHtml = '3天前';
  47. break;
  48. default:
  49. $timeHtml = date('Y-m-d',$sorce_date);
  50. break;
  51. }
  52. return $timeHtml;
  53. }
复制代码


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