PHP获取今天、明天、昨天、上周、本周、上月、本月 的基础方法

<?php
declare (strict_types=1);

namespace app\librarys;

/**
 * Class DateRange 日期范围处理
 * @package app\librarys
 */
class DateRange {

    /**
     * @var
     */
    private static $instance;

    /**
     * @var
     */
    public $timestamp;

    /**
     * @var
     */
    public $currentDate;

    /**
     *
     */
    private function __construct() {
        $this->currentDate = time();
    }

    /**
     * @title __clone
     */
    private function __clone() {

    }

    /**
     * @title  getInstance
     * @return \app\librarys\DateRange
     */
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * @title  setTimestamp 设置时间格式 默认为时间戳, 可传入 date 方法的 时间参数
     *
     * @param $timestamp
     *
     * @return
     */
    public function setTimestamp($timestamp) {
        $this->timestamp = $timestamp;
        return $this;
    }

    /**
     * @title  setCurrentDate
     *
     * @param $date
     *
     * @return \app\librarys\DateRange
     */
    public function setCurrentDate($date) {
        $this->currentDate = $date;
        return $this;
    }

    /**
     * @title  execute 传入时间格式 并返回开始时间和结束时间
     *
     * @param $dateType
     *
     * @return array|false[]|float[]|int[]|string[]
     */
    public final function execute($dateType) {
        switch ($dateType) {
            case 'today': // 今天
                $range = $this->getDateRangeByToday();
                break;
            case 'tomorrow': // 明天
                $range = $this->getDateRangeByTomorrowday();
                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;
    }

    /**
     * @title  getDateRangeByToday 返回今天的时间范围
     * @return false[]|float[]|int[]|string[]
     */
    protected function getDateRangeByToday() {
        return [
            $this->getTime(strtotime(date('Y-m-d', $this->currentDate))),
            $this->getTime(strtotime(date('Y-m-d', $this->currentDate)) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByYesterday 返回昨日的时间范围
     * @return false[]|float[]|int[]|string[]
     */
    protected function getDateRangeByYesterday() {
        $yesterday = strtotime('-1 day', $this->currentDate);
        return [
            $this->getTime(strtotime(date('Y-m-d', $yesterday))),
            $this->getTime(strtotime(date('Y-m-d', $yesterday)) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByTomorrowday 返回明天的时间范围
     * @return array
     */
    protected function getDateRangeByTomorrowday() {
        $yesterday = strtotime('+1 day', $this->currentDate);
        return [
            $this->getTime(strtotime(date('Y-m-d', $yesterday))),
            $this->getTime(strtotime(date('Y-m-d', $yesterday)) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByWeek 获取本周的时间范围
     * @return array
     */
    protected function getDateRangeByWeek() {
        return [
            $this->getTime(strtotime(date('Y-m-d', strtotime('this week Monday', $this->currentDate)))),
            $this->getTime(strtotime(date('Y-m-d', strtotime('this week Sunday', $this->currentDate))) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByLastWeek 获取上周的时间范围
     * @return array
     */
    protected function getDateRangeByLastWeek() {
        return [
            $this->getTime(strtotime(date('Y-m-d', strtotime('last week Monday', $this->currentDate)))),
            $this->getTime(strtotime(date('Y-m-d', strtotime('last week Sunday', $this->currentDate))) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByMonth 获取本月的时间范围
     * @return array
     */
    protected function getDateRangeByMonth() {
        return [
            $this->getTime(strtotime(date('Y-m-1', $this->currentDate))),
            $this->getTime(strtotime(date('Y-m-t', $this->currentDate)) + 86400 - 1)
        ];
    }

    /**
     * @title  getDateRangeByLastMonth 获取上月的时间范围
     * @return false[]|float[]|int[]|string[]
     */
    protected function getDateRangeByLastMonth() {
        return [
            $this->getTime(strtotime(date('Y-m-d', strtotime('first day of previous month', $this->currentDate)))),
            $this->getTime(strtotime(date('Y-m-d', strtotime('last day of previous month', $this->currentDate))) + 86400 - 1)
        ];
    }

    /**
     * @title  getTime 按 $this->timestamp 返回时间格式
     *
     * @param string $time
     *
     * @return false|float|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 (!$timeInt) {
                    $timeInt = time();
                }
                break;
            default:
                $timeInt = time();
        }
        return empty($this->timestamp) ? $timeInt : date($this->timestamp, (int)$timeInt);
    }

    /**
     * @title  getTimeRangeByDateRange 获取给定的开始时间和结束时间的每天时间
     *
     * @param int $start
     * @param int $end
     *
     * @return array|false
     */
    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 * 86400);
        }
        return $day;
    }


}

非特殊说明,本博所有文章均为博主原创。

备注:相关侵权、举报、投诉及建议等,请联系站长

添加新评论

昵称
邮箱
网站