Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php日期时间和时间戳计算_Php_Algorithm_Datetime_Math_For Loop - Fatal编程技术网

Php日期时间和时间戳计算

Php日期时间和时间戳计算,php,algorithm,datetime,math,for-loop,Php,Algorithm,Datetime,Math,For Loop,我试图得到一个日期数组来填充日历,我面临着无限循环的问题 代码如下: $month_length = 1; $day_begin = 9; $day_end = 19; $event_interval = 15; $date = new DateTime(); $today_date_name = $date->format("D"); $today_date_number = $date->format("j"); $today_date_month = $date->f

我试图得到一个日期数组来填充日历,我面临着无限循环的问题

代码如下:

$month_length = 1;
$day_begin = 9;
$day_end = 19;
$event_interval = 15;

$date = new DateTime();

$today_date_name = $date->format("D");
$today_date_number = $date->format("j");
$today_date_month = $date->format("M");
$today_date_month_number =$date->format("n");
$today_date_year = $date->format("Y");


for ($length = 0; $length <= ($month_length - 1); $length++)
{
    $date->modify("+$length month"); 
    $current_date_name = $date->format("D");
    $current_date_number = $date->format("j");
    $current_date_month = $date->format("M");
    $current_date_month_number = $date->format("n");
    $current_date_year = $date->format("Y");

    //calculate the length of the month
    $current_month_length = cal_days_in_month(CAL_GREGORIAN, $current_date_month_number, $current_date_year);

    if($current_date_month_number != $today_date_month_number){
        // if we are not in the current month, start the second loop
        // the first of the month.
        $current_date_number = 1;
    }

    for($current_date_number; $current_date_number <= $current_month_length; $current_date_number++)
    {
        $date->setDate($current_date_year, $current_date_month_number, $current_date_number);

        //set the ending before because of the loop;

        //set the ending of the day
        $date->setTime($day_end, 0, 0);
        //get the timestamp of beginning
        $ending_timestamp = $date->format("U");

        //set the beginning of the day
        $date->setTime($day_begin, 0, 0);
        //get the timestamp of beginning
        $beginning_timestamp = $date->format("U");

        $day_length = $ending_timestamp - $beginning_timestamp;

        //60 seconds for 1min
        $interval = 60 * $event_interval;

        for($the_timestamp = 0; $the_timestamp <= $day_length ; $the_timestamp + $interval)
        {
            $current_timestamp = $beginning_timestamp + $the_timestamp;
            $date->setTimestamp($current_timestamp);
            $final_array[$current_date_year][$current_date_number . " " . $current_date_month][$current_timestamp] = $date->format("H : i");
        }

    }
}
最后一个for循环以无限循环结束,因此达到最大执行时间,必须在30秒以下完成

for($the_timestamp = 0; $the_timestamp <= $day_length ; $the_timestamp + $interval){
    //Stuff
}
这个循环不会增加$u时间戳,这就是它永远循环的原因


最后一部分应该是$thetimestamp+=$interval,它相当于$thetimestamp=$thetimestamp+$interval。

您的代码太复杂了,您做错了什么。以下内容将生成一个表示全年的数组:-

$start = new \DateTime('1st January');
$end = new \DateTime('31st December');
$interval = new \DateInterval('P1D');
$period = new \DatePeriod($start, $interval, $end->modify('+ 1 day'));

$year = array();

foreach($period as $day){
    $year[$day->format('M')][(int)$day->format('d')] = $day->format('D');
}

var_dump($year);

非常感谢,我将根据我的代码调整您的方式。但是P1D的含义是什么?我在哪里可以找到关于它的文档?我通过php.net搜索,但没有结果,就像日期格式化字母一样?看看和