簡體   English   中英

2個日期之間每天,每周,每月,每年的日期數組

[英]Date Array For Every Day, Week, Month, Year in between 2 dates

我正在嘗試使用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,
           )
        )
    )
}

如上所述,唯一的規定是開始日期應從開始日期開始,結束日期應從結束日期開始。

有人可以指出我正確的方向。

干杯。

您的makingQueryArray函數似乎很復雜-我認為您應該以不同的方式解決它。

您可以使用dateperiod獲取兩個日期之間的所有天數:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM