From 081849927d37d0b9c55dad6624c45389c277bc22 Mon Sep 17 00:00:00 2001 From: axios Date: Fri, 17 Jul 2020 14:33:28 +0800 Subject: [PATCH] add Datetime --- src/Datetime.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Helper.php | 41 ++++++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/Datetime.php diff --git a/src/Datetime.php b/src/Datetime.php new file mode 100644 index 0000000..6b3e82f --- /dev/null +++ b/src/Datetime.php @@ -0,0 +1,55 @@ +base_timestamp = $base_timestamp; + } + + public function hourBeginEnd($hour) + { + $date = date('Y-m-d', $this->base_timestamp); + $hour = sprintf('%02d', $hour); + $begin = strtotime($date . ' ' . $hour . ':00:00'); + $end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds'); + + return [$begin, $end]; + } + + public function dayBeginEnd($date = null) + { + if (null === $date) { + $date = date('Y-m-d', $this->base_timestamp); + } + $begin = strtotime($date . ' 00:00:00'); + $end = strtotime("{$date} +1 day -1 seconds"); + + return [$begin, $end]; + } + + public function monthBeginEnd($year = null, $month = null) + { + if (null === $year) { + $year = date('Y', $this->base_timestamp); + } + if (null === $month) { + $month = date('m', $this->base_timestamp); + } + $month = sprintf('%02d', $month); + $ymd = $year . '-' . $month . '-01'; + $begin = strtotime($ymd . ' 00:00:00'); + $end = strtotime("{$ymd} +1 month -1 seconds"); + + return [$begin, $end]; + } +} diff --git a/src/Helper.php b/src/Helper.php index 7970e96..ced0d7b 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -6,11 +6,19 @@ class Helper { + /** + * @param $date + * @param $hour + * @param string $format + * + * @return array + * + * @deprecated use Datetime instead, pls + */ public static function getHourBeginEndTime($date, $hour, $format = 'timestamp') { - $hour = sprintf('%02d', $hour); - $begin = strtotime($date . ' ' . $hour . ':00:00'); - $end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds'); + $datetime = new Datetime(strtotime($date)); + list($begin, $end) = $datetime->hourBeginEnd($hour); if ('timestamp' == $format) { return [ 'begin' => $begin, @@ -24,10 +32,18 @@ public static function getHourBeginEndTime($date, $hour, $format = 'timestamp') ]; } + /** + * @param $date + * @param string $format + * + * @return array + * + * @deprecated use Datetime instead, pls + */ public static function getDayBeginEndTime($date, $format = 'timestamp') { - $begin = strtotime($date . ' 00:00:00'); - $end = strtotime("{$date} +1 day -1 seconds"); + $datetime = new Datetime(); + list($begin, $end) = $datetime->dayBeginEnd($date); if ('timestamp' == $format) { return [ 'begin' => $begin, @@ -41,12 +57,19 @@ public static function getDayBeginEndTime($date, $format = 'timestamp') ]; } + /** + * @param $year + * @param $month + * @param string $format + * + * @return array + * + * @deprecated use Datetime instead, pls + */ public static function getMonthBeginEndDay($year, $month, $format = 'timestamp') { - $month = sprintf('%02d', $month); - $ymd = $year . '-' . $month . '-01'; - $begin = strtotime($ymd . ' 00:00:00'); - $end = strtotime("{$ymd} +1 month -1 seconds"); + $datetime = new Datetime(); + list($begin, $end) = $datetime->monthBeginEnd($year, $month); if ('timestamp' == $format) { return [ 'begin' => $begin,