1. 程式人生 > >獲取周,月,年的起始和結尾

獲取周,月,年的起始和結尾

  訂單查詢需要,所以記錄一下。

 /**
     * @param $zone 周 月 年 按需求更改
     * @param int $interval 間隔值
     * @param bool $tillnow 是否截止到現在
     * @return array
     */
    public function getTimeZone($zone,$interval=0,$tillnow=true)
    {
        $month = (int)date('m'); $week = (int)date('w');
        $year = (int)date('Y'); $today = (int)date('d');

        switch ($zone)
        {
            case $zone=='week':
                $today -=($week +7*$interval);
                $m  =   $month;
                $d  =   $tillnow==true?$today:($today+8);
                $y  =   $year;
                break;
            case $zone=='month':
                $today=1;
                $m  =   $tillnow==true?$month:($month+1);
                $d  =   $today;
                $y  =   $year;
                break;
            case $zone=='year':
                $today= 1;
                $month= 1;
                $m  =   $month;
                $d  =   $today;
                $y  =   $tillnow==true?$year:($year+1);
                break;
        }

        $start = mktime(0,0,0,$month,$today,$year);
        $end   = mktime(0,0,0,$m,$d,$y);

        $redata = ['start'=>$start,'end'=>$end];

        return $redata;


    }