- A+
所属分类:PHP
在开发中,我们往往需要对数据进行统计,而统计离不开时间维度的搜索,因此我们可以建立一个公共的时间类库来生成相关的时间范围。
<?php class StatisticsDate { //环比对应的时间戳 const CHAIN_RATIO = [ 'today' => 'yesterday', 'week' => 'lastWeek', 'month' => 'lastMonth' ]; //单例模式 protected static $instance; //返回的时间格式 private $timestamp; //单例方法 public static function getInstance() { if (!self::$instance instanceof StatisticsDate) { self::$instance = new static(); } return self::$instance; } //设置时间格式 默认为时间戳, 可传入 date 方法的 时间参数 public function setTimestamp($timestamp) { $this->timestamp = $timestamp; return $this; } /** * 传入时间格式 并返回开始时间和结束时间 * @param $dateType * @return array */ public final function execute($dateType) { switch ($dateType) { case 'today': $range = $this->getDateRangeByToday(); break; case 'yesterday': $range = $this->getDateRangeByYesterday(); break; case 'week': $range = $this->getDateRangeByWeek(); break; case 'lastWeek': $range = $this->getDateRangeByLastWeek(); break; case 'month': $range = $this->getDateRangeByMonth(); break; case 'lastMonth': $range = $this->getDateRangeByLastMonth(); break; default: $range = []; } return $range; } /** * 环比时可以用 * @param $dateType * @return array */ public function chainRatioExecute($dateType) { if(!isset(self::CHAIN_RATIO[$dateType])) { return []; } return $this->execute(self::CHAIN_RATIO[$dateType]); } /** * 返回今天的时间范围 * @return array */ protected function getDateRangeByToday() { return [ $this->getTime(mktime(0, 0, 0, date('m'), date('d'), date('Y'))), $this->getTime(mktime(23, 59, 59, date('m'), date('d'), date('Y'))) ]; } /** * 返回昨日的时间范围 * @return array */ protected function getDateRangeByYesterday() { $yesterday = date('d') - 1; return [ $this->getTime(mktime(0, 0, 0, date('m'), $yesterday, date('Y'))), $this->getTime(mktime(23, 59, 59, date('m'), $yesterday, date('Y'))) ]; } /** * 获取本周的时间范围 * @return array */ protected function getDateRangeByWeek() { $timestamp = time(); return [ $this->getTime(strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)))), $this->getTime(strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1) ]; } /** * 获取上周的时间范围 * @return array */ protected function getDateRangeByLastWeek() { $timestamp = time(); return [ $this->getTime(strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp)))), $this->getTime(strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1) ]; } /** * 获取本月的时间范围 * @return array */ protected function getDateRangeByMonth() { return [ $this->getTime(mktime(0, 0, 0, date('m'), 1, date('Y'))), $this->getTime(mktime(23, 59, 59, date('m'), date('t'), date('Y'))) ]; } /** * 获取上月的时间范围 * @return array */ protected function getDateRangeByLastMonth() { $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y')); $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y')); return [$this->getTime($begin), $this->getTime($end)]; } /** * 按 $this->timestamp 返回时间格式 * @param string $time * @return false|int|string */ public function getTime($time = '') { switch (true) { case (empty($time)): $timeInt = time(); break; case (is_numeric($time)): $timeInt = $time; break; case (is_string($time)): $timeInt = strtotime($time); if (false == $timeInt) { $timeInt = time(); } break; default: $timeInt = time(); } return empty($this->timestamp) ? $timeInt : date($this->timestamp, (int)$timeInt); } /** * 获取给定的开始时间和结束时间的每天时间 * @param int $start * @param int $end * @return array|bool */ public function getTimeRangeByDateRange(int $start, int $end) { if($start > $end) { return false; } $diff = ($end - $start)/86400; $day = []; for ($i = 0; $i <= $diff; $i ++) { $day[] = $this->getTime($start + $i* 24 * 60 * 60); } return $day; } }
使用实例:
StatisticsDate::getInstance()->setTimestamp('Y-m-d')->execute('week');