From 79dd3541d439256ff88127e4fc630f7ec699e899 Mon Sep 17 00:00:00 2001 From: joycezhang <787027175@qq.com> Date: Mon, 12 Feb 2024 20:33:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(TimeHelper):=20=E9=87=8D=E6=9E=84D?= =?UTF-8?q?ateHelper=20=E5=8A=A9=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{DateHelper.php => TimeHelper.php} | 158 +++++++++++++----- 1 file changed, 120 insertions(+), 38 deletions(-) rename src/Helpers/{DateHelper.php => TimeHelper.php} (80%) diff --git a/src/Helpers/DateHelper.php b/src/Helpers/TimeHelper.php similarity index 80% rename from src/Helpers/DateHelper.php rename to src/Helpers/TimeHelper.php index a715883..17ec207 100644 --- a/src/Helpers/DateHelper.php +++ b/src/Helpers/TimeHelper.php @@ -18,10 +18,10 @@ /** * 日期常用助手函数 - * Class DateHelper + * Class TimeHelper * @package JoyceZ\LaravelLib\Helpers */ -class DateHelper +class TimeHelper { /** * 获取今日开始时间戳和结束时间戳 @@ -30,9 +30,10 @@ class DateHelper */ public static function today() { + list($y, $m, $d) = explode('-', date('Y-m-d')); return [ - 'start' => mktime(0, 0, 0, (int)date('m'), (int)date('d'), (int)date('Y')), - 'end' => mktime(0, 0, 0, (int)date('m'), (int)date('d') + 1, (int)date('Y')) - 1, + mktime(0, 0, 0, $m, $d, $y), + mktime(23, 59, 59, $m, $d, $y) ]; } @@ -43,9 +44,10 @@ public static function today() */ public static function yesterday() { + $yesterday = date('d') - 1; return [ - 'start' => mktime(0, 0, 0, (int)date('m'), (int)date('d') - 1, (int)date('Y')), - 'end' => mktime(0, 0, 0, (int)date('m'), (int)date('d'), (int)date('Y')) - 1, + mktime(0, 0, 0, date('m'), $yesterday, date('Y')), + mktime(23, 59, 59, date('m'), $yesterday, date('Y')) ]; } @@ -55,15 +57,11 @@ public static function yesterday() */ public static function week() { - $length = 0; - // 星期天直接返回上星期,因为计算周围 星期一到星期天,如果不想直接去掉 - if (date('w') == 0) { - $length = 7; - } - + list($y, $m, $d, $w) = explode('-', date('Y-m-d-w')); + if ($w == 0) $w = 7; return [ - 'start' => mktime(0, 0, 0, (int)date('m'), (int)date('d') - (int)date('w') + 1 - $length, (int)date('Y')), - 'end' => mktime(23, 59, 59, (int)date('m'), (int)date('d') - (int)date('w') + 7 - $length, (int)date('Y')), + mktime(0, 0, 0, $m, $d - $w + 1, $y), + mktime(23, 59, 59, $m, $d - $w + 7, $y) ]; } @@ -74,14 +72,10 @@ public static function week() */ public static function lastWeek() { - $length = 7; - // 星期天直接返回上星期,因为计算周围 星期一到星期天,如果不想直接去掉 - if (date('w') == 0) { - $length = 14; - } + $timestamp = time(); return [ - 'start' => mktime(0, 0, 0, (int)date('m'), (int)date('d') - (int)date('w') + 1 - $length, (int)date('Y')), - 'end' => mktime(23, 59, 59, (int)date('m'), (int)date('d') - (int)date('w') + 7 - $length, (int)date('Y')), + strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))), + strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1 ]; } @@ -92,9 +86,10 @@ public static function lastWeek() */ public static function thisMonth() { + list($y, $m, $t) = explode('-', date('Y-m-t')); return [ - 'start' => mktime(0, 0, 0, (int)date('m'), 1, (int)date('Y')), - 'end' => mktime(23, 59, 59, (int)date('m'), (int)date('t'), (int)date('Y')), + mktime(0, 0, 0, $m, 1, $y), + mktime(23, 59, 59, $m, $t, $y) ]; } @@ -105,18 +100,12 @@ public static function thisMonth() */ public static function lastMonth() { - $year = (int)date('Y'); - $start = mktime(0, 0, 0, (int)date('m') - 1, 1, $year); - $end = mktime(23, 59, 59, (int)date('m') - 1, (int)date('t', $start), $year); - - if (date('m', $start) != date('m', $end)) { - $end -= 60 * 60 * 24; - } + $y = date('Y'); + $m = date('m'); + $begin = mktime(0, 0, 0, $m - 1, 1, $y); + $end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y); - return [ - 'start' => $start, - 'end' => $end, - ]; + return [$begin, $end]; } /** @@ -139,6 +128,99 @@ public static function monthsAgo($month) ]; } + /** + * 返回今年开始和结束的时间戳 + * + * @return array + */ + public static function year() + { + $y = date('Y'); + return [ + mktime(0, 0, 0, 1, 1, $y), + mktime(23, 59, 59, 12, 31, $y) + ]; + } + + /** + * 返回去年开始和结束的时间戳 + * + * @return array + */ + public static function lastYear() + { + $year = date('Y') - 1; + return [ + mktime(0, 0, 0, 1, 1, $year), + mktime(23, 59, 59, 12, 31, $year) + ]; + } + + /** + * 获取几天前零点到现在/昨日结束的时间戳 + * + * @param int $day 天数 + * @param bool $now 返回现在或者昨天结束时间戳 + * @return array + */ + public static function dayToNow($day = 1, $now = true) + { + $end = time(); + if (!$now) { + list($foo, $end) = self::yesterday(); + } + return [ + mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')), + $end + ]; + } + + /** + * 返回几天前的时间戳 + * + * @param int $day + * @return int + */ + public static function daysAgo($day = 1) + { + $nowTime = time(); + return $nowTime - self::daysToSecond($day); + } + + /** + * 返回几天后的时间戳 + * + * @param int $day + * @return int + */ + public static function daysAfter($day = 1, $nowTime = 0) + { + $nowTime = $nowTime ?: time(); + return $nowTime + self::daysToSecond($day); + } + + /** + * 天数转换成秒数 + * + * @param int $day + * @return int + */ + public static function daysToSecond($day = 1) + { + return $day * 86400; + } + + /** + * 周数转换成秒数 + * + * @param int $week + * @return int + */ + public static function weekToSecond($week = 1) + { + return self::daysToSecond() * 7 * $week; + } + /** * 某年 * @param $year @@ -154,8 +236,8 @@ public static function someYear($year) $end_time = date('Y-m-t H:i:s', strtotime($end_month)); return [ - 'start' => strtotime($start_time), - 'end' => strtotime($end_time) + strtotime($start_time), + strtotime($end_time) ]; } @@ -172,8 +254,8 @@ public static function aMonth($year = 0, $month = 0) $month = $month == 0 ? (int)date('m') : $month; $day = date('t', strtotime($year . '-' . $month)); return [ - "start" => strtotime($year . '-' . $month), - "end" => mktime(23, 59, 59, $month, (int)$day, (int)$year) + strtotime($year . '-' . $month), + mktime(23, 59, 59, $month, (int)$day, (int)$year) ]; }