-4

日付の配列を取得してカレンダーに入力しようとしていますが、無限ループの問題に直面しています。

コードは次のとおりです。

$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 秒未満で実行する必要があります。

4

2 に答える 2

1
for($the_timestamp = 0; $the_timestamp <= $day_length ; $the_timestamp + $interval){
    //Stuff
}

このループはインクリメントしない$the_timestampため、永久にループします。

最後の部分は、$the_timestamp += $intervalと同等である必要があり$the_timestamp = $the_timestamp + $intervalます。

于 2013-08-14T10:20:22.280 に答える