From 9ca4e0d2f95ecf5a50ea0bf8418341b10179bc52 Mon Sep 17 00:00:00 2001 From: joycezhang <787027175@qq.com> Date: Mon, 24 Apr 2023 15:32:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(DateHelper):=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=9C=88=E4=BB=BD=E6=97=A5=E6=9C=9F=E6=8C=89?= =?UTF-8?q?=E5=91=A8=E5=88=86=E7=BB=84=EF=BC=8C=E6=8C=87=E5=AE=9A=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E8=8E=B7=E5=8F=96=E5=91=A8=E6=95=B0=E5=92=8C=E5=AD=A3?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/DateHelper.php | 63 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/src/Helpers/DateHelper.php b/src/Helpers/DateHelper.php index fdbff4f..dd4ef25 100644 --- a/src/Helpers/DateHelper.php +++ b/src/Helpers/DateHelper.php @@ -13,6 +13,9 @@ namespace JoyceZ\LaravelLib\Helpers; +use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; + /** * 日期常用助手函数 * Class DateHelper @@ -103,8 +106,8 @@ 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); + $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; @@ -126,7 +129,7 @@ public static function monthsAgo($month) { $year = (int)date('Y'); $start = mktime(0, 0, 0, (int)date('m') - $month, 1, $year); - $end = mktime(23, 59, 59, (int)date('m') - $month, (int)date('t',$start), $year); + $end = mktime(23, 59, 59, (int)date('m') - $month, (int)date('t', $start), $year); if (date('m', $start) != date('m', $end)) { $end -= 60 * 60 * 24; } @@ -402,4 +405,58 @@ public static function isDateTimeCross($beginTime1 = 0, $endTime1 = 0, $beginTim } } + /** + * 指定年月,获取日期,并按每周分组。开始时间周一至周日排序 + * @param $date + * @param string $format + * @return array + * @throws \Exception + */ + public static function getWeekAMonth($date, $format = 'Y-m-d') + { + $time = strtotime($date . '-01'); + $month = date('m', $time); + $carbon = new Carbon(new Carbon(date('Y-m-d', $time), 'Asia/Shanghai')); + $weeks = []; + while (intval($carbon->month) == intval($month)) { + $weeks[$carbon->weekNumberInMonth][] = $carbon->format($format); + $carbon->addDay(); + } + return $weeks; + } + + /** + * 指定年月,获取每周起止日期,并按每周分组 + * @param $date + * @param string $format + * @return array + * @throws \Exception + */ + public static function getWeekStartEndAMoth($date, $format = 'Y-m-d') + { + $weeks = self::getWeekAMonth($date, $format); + $weekData = []; + foreach ($weeks as $key => $item) { + $weekData[$key] = [Arr::first($item), Arr::last($item)]; + } + return $weekData; + } + + /** + * 一个月以周一开始,为7天 + * @param $timestamp 时间 + * @param bool $isWeekMonday true : 一个月以周一开始,为7天;false :一个月以周日开始 + * @return array + */ + public function getWeekAndQInAMonth($timestamp, $isWeekMonday = true) + { + $dt = Carbon::parse($timestamp); + $dt->timezone = "Asia/Shanghai"; + return [ + 'week' => $isWeekMonday ? $dt->weekNumberInMonth : $dt->weekOfMonth, + 'year' => $dt->year, + 'quarter' => intval(($dt->month - 1) / 3 + 1) + ]; + } + } \ No newline at end of file