我想在一个循环中重复一个月的固定次数。如果日期是有效日期,它只会回显日期,否则会跳过日期。 例如,如果“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 –