简体   繁体   中英

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

I am trying to create an array using PHP OOP that basically creates and array for every day, month, year within 2 dates and allocates a ticket and hours worked set at 0 for every single day that has passed.

The code I have used is:

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);

I have been playing around with it for a while and can not get this array to function corectly.

The array should look like this:

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

As stated the only stipulation is that the start date should start at the start date and the end date should start at the end date.

Could someone point me in the right direction.

Cheers.

Your makingQueryArray function seems complicate - I believe you should solve it differently.

You can use dateperiod to get all days between 2 dates:

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

Now You can loop on all days and set you array:

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 will give you your desire output. Live example: 3v4l

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM