带有日期的循环的php超时
我想得到一个周期内的日期数组。为此,我想出了一个for循环(似乎很简单…),但当我运行它时,即使是在1个月的日期,它也会超时 这是我的php:带有日期的循环的php超时,php,date,for-loop,timeout,Php,Date,For Loop,Timeout,我想得到一个周期内的日期数组。为此,我想出了一个for循环(似乎很简单…),但当我运行它时,即使是在1个月的日期,它也会超时 这是我的php: $startdate = '2018-01-31'; $recurring = '2'; switch($recurring) { case '1': $period = '+1 day'; break;
$startdate = '2018-01-31';
$recurring = '2';
switch($recurring) {
case '1':
$period = '+1 day';
break;
case '2':
$period = '+1 week';
break;
case '3':
$period = '+1 month';
break;
case '4':
$period = '+3 months';
break;
case '5':
$perion = '+1 year';
break;
default:
$period = null;
break;
}
$dates = [];
if($period !== null) {
for($date = $startdate; $date < strtotime('+1 month', $startdate); strtotime($period, $date)) {
$dates[] = $date;
}
}
echo json_encode($dates);
$startdate='2018-01-31';
$recurtive='2';
交换机(循环){
案例“1”:
$period=“+1天”;
打破
案例“2”:
$period=“+1周”;
打破
案例“3”:
$period=“+1个月”;
打破
案例“4”:
$period='3个月';
打破
案例“5”:
$perion=“+1年”;
打破
违约:
$period=null;
打破
}
$dates=[];
如果($period!==null){
对于($date=$startdate;$date
在for循环的增量部分增加$date
和$date=strotime($period,$date)
应该可以防止超时,但是还可以做一些其他改进
首先,我建议在循环之前计算一次结束日期,以避免每次检查延续条件时额外的strotime
调用
$end = strtotime("$startdate +1 month");
然后,在初始化部分设置$date=strottime($startdate)
,否则您将得到一个日期字符串而不是时间戳,作为$dates
数组中的第一个值
for ($date = strtotime($startdate); $date < $end; $date = strtotime($period, $date)) {
$dates[] = $date;
}
for($date=strottime($startdate);$date<$end;$date=strottime($period,$date)){
$dates[]=$date;
}
在该循环中,$date
在哪里增加?strotime($period,$date)
$period有增量检查文档中的strotime
。它是否修改作为第二个参数传递的时间戳?对。。。当然不是。你知道怎么做吗?我找到了。。将strotime($period,$date)更改为$date=strotime($period,$date)
成功了。谢谢@别生气