2个日期之间每天,每周,每月,每年的日期数组

时间:2019-03-26 15:29:00

标签: php arrays oop

我正在尝试使用PHP OOP创建一个数组,该数组基本上在2个日期内为每天,每个月,每年创建和数组,并为经过的每一天分配一个工单和工作时间设置为0。

我使用的代码是:

class statisticsClass{

    private $DateFrom       = null;
    private $DateTo         = null;

    private $startYear      = null;
    private $startMonth     = null;
    private $startDay       = null;

    private $endYear        = null;
    private $endMonth       = null;
    private $endDay         = null;

    private $theDateDif     = null;
    private $stages         = null;

    private function makeDates(){

        if(!empty($this->DateFrom)){ 

            $this->DateFrom = $this->generals->convertDate($this->DateFrom); 

        } else {

            $this->DateFrom = date('Y-m-d', strtotime('last year')) . ' 00:00:00';

        }

        if(!empty($this->DateTo)){ 

            $this->DateTo = $this->generals->convertDate($this->DateTo); 

        } else {

            $this->DateTo   = date('Y-m-d', strtotime('now')) . ' 23:59:59';

        }

        list($theDate, $theTime) = explode(' ', $this->DateFrom);
        list($this->startYear, $this->startMonth, $this->startDay) = explode('-', $theDate);

        list($theDate2, $theTime2) = explode(' ', $this->DateTo);
        list($this->endYear, $this->endMonth, $this->endDay) = explode('-', $theDate2);

    }

    private function removeZeros($number){

        return ltrim($number, '0');

    }

    private function chartItemVersion(){

    if($this->theDateDif <= 31){

        $this->stages = 'daily';

    } elseif($this->theDateDif > 31 && $this->theDateDif <= 183){

        $this->stages = 'weekly';

    } else {

        $this->stages = 'monthly';

    }

}

    private function addZeros($number){

        if($number < 10){

            return '0' . $number;

        }

        return $number;

    }

    private function makingQueryArray(){

        for($yearMake=$this->startYear; $yearMake<=$this->endYear; $yearMake++){

            $this->queryArray[] = intval($yearMake);

        }

        foreach($this->queryArray as $year){

            if($year === $this->startYear){

                $currentMonth = intval($this->removeZeros($this->startMonth));

                for($currentMonth; $currentMonth <= 12;$currentMonth++){

                    $this->queryArray[$year][] = (string)$this->addZeros($currentMonth);
                    $tempHowManyDays = cal_days_in_month(CAL_GREGORIAN, $this->addZeros($currentMonth), $year);

                    if($this->startYear === $this->addZeros($currentMonth)){

                        $currentDay = intval($this->removeZeros($this->startDay));

                        for($currentDay; $currentDay <= $tempHowManyDays; $currentDay){

                            $this->queryArray[$year][(string)$this->addZeros($currentMonth)][(string)$this->addZeros($currentDay)]['ticket'] = 0;
                            $this->queryArray[$year][(string)$this->addZeros($currentMonth)][(string)$this->addZeros($currentDay)]['Hours'] = 0;

                        }

                    } else {

                        for($i=1; $i<=$tempHowManyDays; $i++){

                            $this->queryArray[$year][(string)$this->addZeros($currentMonth)][(string)$this->addZeros($i)]['ticket'] = 0;
                            $this->queryArray[$year][(string)$this->addZeros($currentMonth)][(string)$this->addZeros($i)]['Hours'] = 0;
                        }

                    }

                }


            } elseif($year === $this->endYear) {

                $endMonth = intval($this->removeZeros($this->endMonth));

                for($a=1; $a <= $endMonth; $a++){

                    $this->queryArray[$year][] = (string)$this->addZeros($a);

                    if($a === $endMonth){

                        $tempHowManyDays = intval($this->removeZeros($this->endDay));

                    } else {

                        $tempHowManyDays = cal_days_in_month(CAL_GREGORIAN, $this->addZeros($a), $year);

                    }

                    for($b=1; $b<=$tempHowManyDays; $b++){

                        $this->queryArray[$year][(string)$this->addZeros($a)][(string)$this->addZeros($b)]['ticket'] = 0;
                        $this->queryArray[$year][(string)$this->addZeros($a)][(string)$this->addZeros($b)]['Hours'] = 0;

                    }

                }

            } else {

                for($a=1; $a <= 12; $a++){

                    $this->queryArray[$year][] = (string)$this->addZeros($a);
                    $tempHowManyDays = cal_days_in_month(CAL_GREGORIAN, $this->addZeros($a), $year);

                    for($b=1; $b<=$tempHowManyDays; $b++){

                        $this->queryArray[$year][(string)$this->addZeros($a)][(string)$this->addZeros($b)]['ticket'] = 0;
                        $this->queryArray[$year][(string)$this->addZeros($a)][(string)$this->addZeros($b)]['Hours'] = 0;

                    }

                }


            }

        }

        var_dump($this->queryArray);

    }

    private function dateDifference(){

        $now        = strtotime($this->DateFrom);
        $your_date  = strtotime($this->DateTo);
        $datediff   = $your_date - $now;

        $this->theDateDif = round($datediff / (60 * 60 * 24));

    }

    function __construct($pdo, $theArray, $generals = null){

        if($generals !== null){ $this->generals = $generals; }

        $this->pdo = $pdo;

        if(isset($theArray['DateFrom']) && !empty($theArray['DateFrom'])){ $this->DateFrom = $theArray['DateFrom']; }
        if(isset($theArray['DateTo']) && !empty($theArray['DateTo'])){ $this->DateTo = $theArray['DateTo']; }

        $this->makeDates();
        $this->dateDifference();
        $this->chartItemVersion();
        $this->makingQueryArray();

        var_dump($this->queryArray);

    }

}

$theArray = array();

$amhStatistics = new statisticsClass($pdo, $theArray, $generals);

我已经使用它一段时间了,无法使该数组发挥核心作用。

该数组应如下所示:

array(){
    '2018' => array(
        01 => array(
           01 => array(
              'ticket' => 0,
              'hours' => 0,
           ),
           02 => array(
              'ticket' => 0,
              'hours' => 0,
           )
        )
    )
}

如前所述,唯一的规定是开始日期应从开始日期开始,结束日期应从结束日期开始。

有人能指出我正确的方向吗?

干杯。

1 个答案:

答案 0 :(得分:2)

您的makingQueryArray函数似乎很复杂-我认为您应该以不同的方式解决它。

您可以使用dateperiod获取2个日期之间的所有天数:

$startDate = '2010-10-01';
$endDate = '2010-10-05';
$period = new DatePeriod(new DateTime($startDate), new DateInterval('P1D'), new DateTime($endDate));

现在,您可以全天循环并设置数组:

foreach ($period as $key => $value) {
   $d = explode("-", $value->format('Y-m-d'));
   $res[$d[0]][$d[1]][$d[2]] = array('ticket' => 0, 'hours' => 0);
}

$res将为您提供期望的输出。实时示例:3v4l