Dateset.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare (strict_types = 1);
  3. namespace dateset;
  4. /**
  5. * 日期时间处理类
  6. */
  7. class Dateset
  8. {
  9. const YEAR = 31536000;
  10. const MONTH = 2592000;
  11. const WEEK = 604800;
  12. const DAY = 86400;
  13. const HOUR = 3600;
  14. const MINUTE = 60;
  15. /**
  16. * 间隔时间段格式化
  17. * @param int $time 时间戳
  18. * @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
  19. * @return string
  20. */
  21. function time_trans($time, $format = 'd')
  22. {
  23. $now = time();
  24. $diff = $now - $time;
  25. if ($diff < 60) {
  26. return '1分钟前';
  27. } else if ($diff < 3600) {
  28. return floor($diff / 60) . '分钟前';
  29. } else if ($diff < 86400) {
  30. return floor($diff / 3600) . '小时前';
  31. }
  32. $yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
  33. $yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
  34. $two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
  35. $three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
  36. $four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
  37. $five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
  38. $six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
  39. $seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
  40. if ($time > $yes_start_time && $time < $yes_end_time) {
  41. return '昨天';
  42. }
  43. if ($time > $yes_start_time && $time < $two_end_time) {
  44. return '1天前';
  45. }
  46. if ($time > $yes_start_time && $time < $three_end_time) {
  47. return '2天前';
  48. }
  49. if ($time > $yes_start_time && $time < $four_end_time) {
  50. return '3天前';
  51. }
  52. if ($time > $yes_start_time && $time < $five_end_time) {
  53. return '4天前';
  54. }
  55. if ($time > $yes_start_time && $time < $six_end_time) {
  56. return '5天前';
  57. }
  58. if ($time > $yes_start_time && $time < $seven_end_time) {
  59. return '6天前';
  60. }
  61. switch ($format) {
  62. case 'd':
  63. $show_time = date('Y-m-d', $time);
  64. break;
  65. case 'i':
  66. $show_time = date('Y-m-d H:i', $time);
  67. break;
  68. case 's':
  69. $show_time = date('Y-m-d H:i:s', $time);
  70. break;
  71. }
  72. return $show_time;
  73. }
  74. /**
  75. * 计算两个时间戳之间相差的时间
  76. *
  77. * $differ = self::differ(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
  78. * $differ = self::differ(60, 182, 'minutes'); // 2
  79. *
  80. * @param int $remote timestamp to find the span of
  81. * @param int $local timestamp to use as the baseline
  82. * @param string $output formatting string
  83. * @return string when only a single output is requested
  84. * @return array associative list of all outputs requested
  85. * @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
  86. */
  87. public static function differ($remote, $local = null, $output = 'years,months,weeks,days,hours,minutes,seconds')
  88. {
  89. // Normalize output
  90. $output = trim(strtolower((string)$output));
  91. if (!$output) {
  92. // Invalid output
  93. return false;
  94. }
  95. // Array with the output formats
  96. $output = preg_split('/[^a-z]+/', $output);
  97. // Convert the list of outputs to an associative array
  98. $output = array_combine($output, array_fill(0, count($output), 0));
  99. // Make the output values into keys
  100. extract(array_flip($output), EXTR_SKIP);
  101. if ($local === null) {
  102. // Calculate the span from the current time
  103. $local = time();
  104. }
  105. // Calculate timespan (seconds)
  106. $timespan = abs($remote - $local);
  107. if (isset($output['years'])) {
  108. $timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
  109. }
  110. if (isset($output['months'])) {
  111. $timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
  112. }
  113. if (isset($output['weeks'])) {
  114. $timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
  115. }
  116. if (isset($output['days'])) {
  117. $timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
  118. }
  119. if (isset($output['hours'])) {
  120. $timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
  121. }
  122. if (isset($output['minutes'])) {
  123. $timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
  124. }
  125. // Seconds ago, 1
  126. if (isset($output['seconds'])) {
  127. $output['seconds'] = $timespan;
  128. }
  129. if (count($output) === 1) {
  130. // Only a single output was requested, return it
  131. return array_pop($output);
  132. }
  133. // Return array
  134. return $output;
  135. }
  136. /**
  137. * 获取指定年月拥有的天数
  138. * @param int $month
  139. * @param int $year
  140. * @return false|int|string
  141. */
  142. public static function days_in_month($month, $year)
  143. {
  144. if (function_exists("cal_days_in_month")) {
  145. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  146. } else {
  147. return date('t', mktime(0, 0, 0, $month, 1, $year));
  148. }
  149. }
  150. }