ループ内で決まった回数だけ日付を月単位で繰り返そうとしています。日付が有効な日付である場合にのみ日付をエコーし、そうでない場合は日付をスキップします。たとえば、「2012-02-30 10:10:00」がループに入ってくる場合、それは無視されてスキップされ、次の反復に進みます。
以下のコードは、start_date "2011-11-07 10:15:00"に対しては正常に機能しますが、コメント付きのstart_date、つまり "2011-11-30 10:15:00"に変更すると、24時間後に追加されます。 1回の反復をスキップします。
どこが間違っているのかわかりません。
<?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 />";
}
?>
コメントされたstart_dateの期待される結果は次のとおりです。
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