我想在一個循環中重複一個月的固定次數。如果日期是有效日期,它只會回顯日期,否則會跳過日期。 例如,如果「2012-02-30 10:10:00」正在循環中,它將被忽略並跳過,並且它將轉到下一次迭代。在PHP中處理日期
下面的代碼適用於start_date「2011-11-07 10:15:00」,但是當我將其更改爲start_date,即「2011-11-30 10:15:00」它添加跳過一個迭代24小時後。
我不知道我哪裏出錯了。
<?php
//$start_date = "2011-11-30 10:15:00";
$start_date = "2011-11-07 10:15:00";
$no_of_repeat = 15;
$repeat_timing = 0;
for($x=0; $x<$no_of_repeat; $x++) {
$start_date = date('Y-m-d G:i:s',(strtotime($start_date) + ($repeat_timing)));
if(date("m",strtotime($start_date)) != 12) {
$next_month = date('m',strtotime($start_date)) + 1;
$year = date('Y',strtotime($start_date));
} else {
$next_month = 1 ;
$year = date('Y',strtotime($start_date)) + 1;
}
$day = date("d",strtotime($start_date));
$next_date =
$year."-".
$next_month."-".
$day." ".
date("G",strtotime($start_date)).":".
date("i",strtotime($start_date)).":".
date("s",strtotime($start_date));
if(checkdate($next_month,$day,$year))
$repeat_timing = strtotime($next_date) - strtotime($start_date);
else
continue;
echo $next_date."<br />";
}
?>
的評論起始日期預期結果如下:
2011-12-30 10:15:00
2012-1-30 10:15:00
2012-3-30 10:15:00
2012-4-30 10:15:00
2012-5-30 10:15:00
2012-6-30 10:15:00
2012-7-30 10:15:00
2012-8-30 10:15:00
2012-9-30 10:15:00
2012-10-30 10:15:00
2012-11-30 10:15:00
2012-12-30 10:15:00
2013-1-30 10:15:00
您顯示預期的結果,但是您得到的結果是什麼?你可以發佈嗎? – DesignerGuy
只需執行上面給出的腳本並解開註釋的start_date並評論當前的start_date –