DateHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/9/22
  12. */
  13. namespace lemo\helper;
  14. use DateTime;
  15. use DateTimeZone;
  16. /**
  17. * 日期时间处理类
  18. */
  19. class DateHelper
  20. {
  21. /**
  22. * @param $time
  23. * @return false|string
  24. * 获取当前日期时间
  25. */
  26. public static function intToDate($time){
  27. return date('Y-m-d H:i:s',$time);
  28. }
  29. /**
  30. * 日期转时间戳
  31. *
  32. * @param $value
  33. * @return false|int
  34. */
  35. public static function dateToInt($value)
  36. {
  37. if (empty($value)) {
  38. return $value;
  39. }
  40. if (!is_numeric($value)) {
  41. return strtotime($value);
  42. }
  43. return $value;
  44. }
  45. /**
  46. * @param $posttime
  47. * @return string
  48. * 多少天前
  49. */
  50. public static function timeAgo($posttime){
  51. //当前时间的时间戳
  52. $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
  53. //之前时间参数的时间戳
  54. $posttimes = strtotime($posttime);
  55. //相差时间戳
  56. $counttime = $nowtimes - $posttimes;
  57. //进行时间转换
  58. if($counttime<=10){
  59. return '刚刚';
  60. }else if($counttime>10 && $counttime<=30){
  61. return '刚才';
  62. }else if($counttime>30 && $counttime<=60){
  63. return '刚一会';
  64. }else if($counttime>60 && $counttime<=120){
  65. return '1分钟前';
  66. }else if($counttime>120 && $counttime<=180){
  67. return '2分钟前';
  68. }else if($counttime>180 && $counttime<3600){
  69. return intval(($counttime/60)).'分钟前';
  70. }else if($counttime>=3600 && $counttime<3600*24){
  71. return intval(($counttime/3600)).'小时前';
  72. }else if($counttime>=3600*24 && $counttime<3600*24*2){
  73. return '昨天';
  74. }else if($counttime>=3600*24*2 && $counttime<3600*24*3){
  75. return '前天';
  76. }else if($counttime>=3600*24*3 && $counttime<=3600*24*20){
  77. return intval(($counttime/(3600*24))).'天前';
  78. }else{
  79. return $posttime;
  80. }
  81. }
  82. /**
  83. * 格式化 UNIX 时间戳为人易读的字符串
  84. * @param int Unix 时间戳
  85. * @param mixed $local 本地时间
  86. * @return string 格式化的日期字符串
  87. */
  88. public static function humanDate($remote, $local = null)
  89. {
  90. $timediff = (is_null($local) || $local ? time() : $local) - $remote;
  91. $chunks = array(
  92. array(60 * 60 * 24 * 365, 'year'),
  93. array(60 * 60 * 24 * 30, 'month'),
  94. array(60 * 60 * 24 * 7, 'week'),
  95. array(60 * 60 * 24, 'day'),
  96. array(60 * 60, 'hour'),
  97. array(60, 'minute'),
  98. array(1, 'second')
  99. );
  100. for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  101. $seconds = $chunks[$i][0];
  102. $name = $chunks[$i][1];
  103. if (($count = floor($timediff / $seconds)) != 0) {
  104. break;
  105. }
  106. }
  107. return lang("%d {$name}%s ago", $count, ($count > 1 ? 's' : ''));
  108. }
  109. }