获取一段时间内,以月/季度为单位,第N天在各个月/季度是几几年几月几号

    /**
     * 获取一段时间内(可跨年),以季度为单位,第N天在各个季度是几月几号
     * @param $sTime        时间戳
     * @param $eTime        时间戳
     * @param $number       第N天
     * @return array
     * @author fengzi
     * @date   2022-11-17 21:16
     */
    public function getQuarterDate($sTime, $eTime, $number) {
        $dates = [];
        $sDate = date('Y-m-d', $sTime);
        $eDate = date('Y-m-d', $eTime);
        for( $i = $sTime; $i <= $eTime; $i = strtotime('+3 month', $i) )
        {
            $tempNumber = $number;
            $monthNum   = date('n', $i);
            $yearNumber = date('Y', $i);
            //获取所在季度的第一个月份
            $startMonthNumber = (($this->getQuarterByMonth($monthNum) - 1) * 3) + 1;

            if ( isset($dayCountByYear[$yearNumber]) ) {
                $dayCountByMonth = $dayCountByYear[$yearNumber];
            } else {
                //获取当前年份的各个月份的天数
                $dayCountByMonth = $this->getMonthDays($yearNumber);
                $dayCountByYear[$yearNumber] = $dayCountByMonth;
            }

            for ($j=$startMonthNumber; $j<=($startMonthNumber+2); $j++) {
                $diff = bcsub($tempNumber, $dayCountByMonth[$j]);
                if ( $diff > 0 ) {
                    $tempNumber = $diff;
                } elseif ( $diff <= 0 ) {
                    $jMonth = str_pad($j, 2, '0', STR_PAD_LEFT);//补充前导零
                    $tempNumber = str_pad($tempNumber, 2, '0', STR_PAD_LEFT); //补充前导零
                    $tempDate = $yearNumber.'-'.$jMonth.'-'.$tempNumber;
                    if ( $sDate <= $tempDate && $tempDate <= $eDate ) {
                        $dates[$yearNumber][] = $tempDate;
                    }
                    break;
                }
            }
        }

        return $dates;
    }



    /**
     * 获取一段时间内(可跨年),以月为单位,第N天在各个月份是几月几号
     * @param $sTime        时间戳
     * @param $eTime        时间戳
     * @param $number       第N天
     * @return array
     * @author fengzi
     * @date   2022-11-18 18:41
     */
    public function getMonthDate($sTime, $eTime, $number)
    {
        $dates = [];

        for( $i = $sTime; $i <= $eTime; $i = strtotime('+1 month', $i) )
        {
            $yearNumber       = date('Y', $i);
            if ( isset($dayCountByYear[$yearNumber]) ) {
                $dayCountByMonth = $dayCountByYear[$yearNumber];
            } else {
                $dayCountByMonth = $this->getMonthDays($yearNumber);
                $dayCountByYear[$yearNumber] = $dayCountByMonth;
            }

            //获取当前时间的月份
            $monthNumber      = date('n', $i);
            $oneMonthTotalNum = $dayCountByMonth[$monthNumber];

            if ( $i == $sTime ) {
                //获取当前时间的天(几号)
                $sDay  = date('j', $sTime);
                if ( $number >= $sDay && $sDay <= $oneMonthTotalNum ) {
                    $dates[$yearNumber][] =  $yearNumber.'-'.$monthNumber.'-'.$number;
                }
            } elseif ( $i == $eTime ) {
                //获取当前时间的天(几号)
                $eDay  = date('j', $eTime);
                if ( $number <= $eDay && $eDay >=1 ) {
                    $dates[$yearNumber][] =  $yearNumber.'-'.$monthNumber.'-'.$number;
                }
            } else {
                if ( 1 <= $number && $number <=$oneMonthTotalNum ) {
                    $dates[$yearNumber][] = date('Y-m-'.$number, $i);
                }
            }
        }

        return $dates;
    }



  /**
     * 获取某年的各个月份分别有几天
     * @param $year     年份,如:2022
     * @return array   输入年份的每个月的天数
     * @author fengzi
     * @date   2022-11-18 18:38
     */
    public function getMonthDays($year)
    {
        $dayCountByMonth = [];

        for ($i=1; $i<=12; $i++) {
            $dayCountByMonth[$i] = date("t", strtotime($year . "-" . $i . "-01"));
        }

        return $dayCountByMonth;
    }



   /**
     * 获取月份所在季度的第一个月份
     * @param $monthNumber
     * @return int
     * @author fengzi
     * @date   2022-11-17 20:57
     */
    public function getQuarterByMonth($monthNumber) {
        return floor(($monthNumber - 1) / 3) + 1;
    }

 

posted @ 2022-11-28 09:55  疯子丶pony  阅读(46)  评论(0编辑  收藏  举报