DateHelper.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace common\helpers;
  3. /**
  4. * 日期数据格式返回
  5. *
  6. * Class DateHelper
  7. * @package common\helpers
  8. * @author qimall
  9. */
  10. class DateHelper
  11. {
  12. /**
  13. * 获取今日开始时间戳和结束时间戳
  14. *
  15. * 语法:mktime(hour,minute,second,month,day,year) => (小时,分钟,秒,月份,天,年)
  16. */
  17. public static function today()
  18. {
  19. return [
  20. 'start' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
  21. 'end' => mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1,
  22. ];
  23. }
  24. /**
  25. * 昨日
  26. *
  27. * @return array
  28. */
  29. public static function yesterDay()
  30. {
  31. return [
  32. 'start' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
  33. 'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
  34. ];
  35. }
  36. /**
  37. * @name:
  38. * @msg: 获取前几天 or 后几天的开始 和 结束时间戳
  39. * @param {int} $number 偏离天数
  40. * @param {string} $type 获取类型, before:从今天往前拿;after:从今天往后拿
  41. * @return array
  42. */
  43. public static function someDays(int $number, string $type = 'before')
  44. {
  45. if ('after' == $type) {
  46. $end = mktime(0, 0, 0, date('m'), date('d') + $number, date('Y')) - 1;
  47. $start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  48. } else {
  49. $start = mktime(0, 0, 0, date('m'), date('d') - $number, date('Y'));
  50. $end = strtotime(date('Ymd', $start) . ' 23:59:59');
  51. }
  52. return compact('start', 'end');
  53. }
  54. /**
  55. * 这周
  56. *
  57. * @return array
  58. */
  59. public static function thisWeek()
  60. {
  61. return [
  62. 'start' => mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')),
  63. 'end' => mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')),
  64. ];
  65. }
  66. /**
  67. * 上周
  68. *
  69. * @return array
  70. */
  71. public static function lastWeek()
  72. {
  73. return [
  74. 'start' => mktime(0, 0, 0,date('m'),date('d') - date('w') + 1 - 7, date('Y')),
  75. 'end' => mktime(23, 59, 59,date('m'),date('d') - date('w') + 7 - 7, date('Y')),
  76. ];
  77. }
  78. /**
  79. * 本月
  80. *
  81. * @return array
  82. */
  83. public static function thisMonth()
  84. {
  85. return [
  86. 'start' => mktime(0, 0, 0, date('m'), 1, date('Y')),
  87. 'end' => mktime(23, 59, 59, date('m'), date('t'), date('Y')),
  88. ];
  89. }
  90. /**
  91. * 上个月
  92. *
  93. * @return array
  94. */
  95. public static function lastMonth()
  96. {
  97. $start = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  98. $end = mktime(23, 59, 59, date('m') - 1, date('t',$start), date('Y'));
  99. if (date('m', $start) != date('m', $end))
  100. {
  101. $end -= 60 * 60 * 24;
  102. }
  103. return [
  104. 'start' => $start,
  105. 'end' => $end,
  106. ];
  107. }
  108. public static function lastYear()
  109. {
  110. $year = date("Y",strtotime("-1 year"));
  111. $start = strtotime($year.'-01-01 00:00:00');
  112. $end = strtotime($year.'-12-31 23:59:59');
  113. return [
  114. 'start' => $start,
  115. 'end' => $end,
  116. ];
  117. }
  118. public static function lastSeason(){
  119. $season = ceil((date('n'))/3)-1;//上季度是第几季度
  120. return [
  121. 'start'=>strtotime(date('Y-m-d H:i:s', mktime(0, 0,0,$season*3-3+1,1,date('Y')))),
  122. 'end'=>strtotime(date('Y-m-d H:i:s',mktime(23,59,59,$season*3,date('t',mktime(0, 0 ,0,$season*3,1,date("Y"))),date('Y')))),
  123. ];
  124. }
  125. /**
  126. * 几个月前
  127. *
  128. * @param integer $month 月份
  129. * @return array
  130. */
  131. public static function monthsAgo($month)
  132. {
  133. return [
  134. 'start' => mktime(0, 0, 0, date('m') - $month, 1, date('Y')),
  135. 'end' => mktime(23, 59, 59, date('m') - $month, date('t'), date('Y')),
  136. ];
  137. }
  138. public static function endOfMonth($month){
  139. return mktime(23,59,59,date('m',strtotime($month))+1,00); //指定月份月末时间戳
  140. }
  141. public static function thisSeason(){
  142. $season = ceil((date('n'))/3);//当月是第几季度
  143. return [
  144. 'start' => mktime(0, 0, 0,$season*3-3+1,1,date('Y')),
  145. 'end' =>mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')),
  146. ];
  147. }
  148. /**
  149. * 格式化时间戳
  150. *
  151. * @param $time
  152. * @return string
  153. */
  154. public static function formatTimestamp($time)
  155. {
  156. $min = $time / 60;
  157. $hours = $time / 60;
  158. $days = floor($hours / 24);
  159. $hours = floor($hours - ($days * 24));
  160. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  161. return $days . " 天 " . $hours . " 小时 " . $min . " 分钟 ";
  162. }
  163. /**
  164. * 时间戳
  165. *
  166. * @param integer $accuracy 精度 默认微妙
  167. * @return int
  168. */
  169. public static function getMicrotime($accuracy = 1000000)
  170. {
  171. $microtime = explode(' ', microtime());
  172. return $microtime = (int)round(($microtime[1] + $microtime[0]) * $accuracy, 0);
  173. }
  174. /**
  175. * 时间格式
  176. * @param int $time
  177. * @return string 2023-11-09(周四) 18:30
  178. */
  179. public static function reserveAt(int $time)
  180. {
  181. $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  182. $week = date('w', $time);
  183. return date('Y-m-d', $time) . "($week_txt[$week]) " . date("H:i", $time);
  184. }
  185. /**
  186. * 获取日期中文名
  187. * @param int $time
  188. * @return string
  189. */
  190. public static function dayChineseText(int $time)
  191. {
  192. $texts = ['今天', ' 明天', '后天'];
  193. $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  194. $index = -1;
  195. for ($i = 0; $i < 3; $i++) {
  196. $i_time = strtotime(date('Y-m-d', time() + ($i * 86400)));
  197. if ($i_time == $time) {
  198. $index = $i;
  199. break;
  200. }
  201. }
  202. if (!empty($texts[$index])) return $texts[$index];
  203. return date("Y-m-d", $time) . "({$week_txt[date('w', $time)]}) " . date("H:i", $time);
  204. }
  205. public static function getMonthLastDayTime($start_time)
  206. {
  207. $mdays = date('t', $start_time);
  208. return strtotime(date('Y-m-' . $mdays . ' 23:59:59', $start_time));
  209. }
  210. public static function getWeekByDate($date)
  211. {
  212. $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  213. $week = date('w', strtotime($date));
  214. return $week_txt[$week];
  215. }
  216. /**
  217. * 10000 转为 10:00
  218. * @param $intHourMin
  219. * @return false|string
  220. */
  221. public static function intHourMinToTime($intHourMin)
  222. {
  223. $time = str_pad($intHourMin, 4, '0', STR_PAD_LEFT);
  224. return date('H:i', strtotime("20241225{$time}00"));
  225. }
  226. public static function time2Int($time)
  227. {
  228. return (int) str_replace(':', '', $time);
  229. }
  230. public static function formatTime2Custom($timestamp)
  231. {
  232. $hour = date('H', $timestamp); // 获取小时部分
  233. // 根据时间段判断对应的中文表示
  234. if ($hour >= 0 && $hour < 6) {
  235. $timePeriod = '凌晨';
  236. } elseif ($hour >= 6 && $hour < 9) {
  237. $timePeriod = '早上';
  238. } elseif ($hour >= 9 && $hour < 12) {
  239. $timePeriod = '上午';
  240. } elseif ($hour == 12) {
  241. $timePeriod = '中午';
  242. } elseif ($hour > 12 && $hour < 18) {
  243. $timePeriod = '下午';
  244. } else {
  245. $timePeriod = '晚上';
  246. }
  247. $w = self::getWeekByDate(date('Y-m-d', $timestamp));
  248. // 格式化时间
  249. return date('Y-m-d', $timestamp) . ' ' . $timePeriod . ' ' . date('H:i', $timestamp);
  250. }
  251. public static function formatTime2CustomW($timestamp)
  252. {
  253. $hour = date('H', $timestamp); // 获取小时部分
  254. // 根据时间段判断对应的中文表示
  255. if ($hour >= 0 && $hour < 6) {
  256. $timePeriod = '凌晨';
  257. } elseif ($hour >= 6 && $hour < 9) {
  258. $timePeriod = '早上';
  259. } elseif ($hour >= 9 && $hour < 12) {
  260. $timePeriod = '上午';
  261. } elseif ($hour == 12) {
  262. $timePeriod = '中午';
  263. } elseif ($hour > 12 && $hour < 18) {
  264. $timePeriod = '下午';
  265. } else {
  266. $timePeriod = '晚上';
  267. }
  268. $w = self::getWeekByDate(date('Y-m-d', $timestamp));
  269. // 格式化时间
  270. return date('Y-m-d', $timestamp) . ' ('. $w . ') ' . $timePeriod;
  271. }
  272. }