简体   繁体   English

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

[英]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. 我正在尝试使用PHP OOP创建一个数组,该数组基本上在2个日期内为每个日,月,年创建和数组,并为已过去的每一天分配票证和工作时间设置为0。

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. 您的makingQueryArray函数似乎很复杂-我认为您应该以不同的方式解决它。

You can use dateperiod to get all days between 2 dates: 您可以使用dateperiod获取两个日期之间的所有天数:

$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. $res将为您提供所需的输出。 Live example: 3v4l 直播示例: 3v4l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 现有的脚本可以按日,周,月,年循环遍历日期范围? - Existing script to loop through date range by day, week, month, year? 如何从php中的年,月,月中的星期几和星期几中获取日期 - How to get the date from year, month, week of the month, and day of the week in php 每月固定日期之间的PHP日期 - PHP date between fixed day of every month 使用星期和年份获取一个月中的某一天的问题 - Issue with getting the day of a month with the week day and year 计算月,年,星期和星期数的日期 - Calculate day of month with month, year, day of week and number of week 使用PHP获取给定月份和年份的一个月中每周的总日数 - Get total day for every week in a month by given month and year using PHP Javascript按照年份DESC,日期ASC,日期ASC排列数组中的日期 - Javascript sort dates in array by year DESC, then month ASC, then day ASC 在从html表单获取的日期之间使用php从mysql中选择数据,但日期存储在日,月,年的3个不同列中 - Select data from mysql using php in between dates got from html form, but date is stored in 3 different columns under day, month, year 在PHP中的年份,星期数和星期几 - Year, number of week and day of week to date in php 在PHP中的两个日期上获取年,月,周,日,时,分,秒间隔 - Get the year, month week, day, hour, minute, second interval on two dates in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM