【发布时间】:2018-08-09 20:53:08
【问题描述】:
我想获得一个时期内的日期数组。为此,我想出了一个 for 循环(看起来很简单......)但是当我运行它时,即使是 1 个月的日期,它也会超时。
这是我的 php:
$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);
【问题讨论】:
-
在那个循环中你在哪里增加
$date? -
strtotime($period, $date)$period 有增量 -
查看
strtotime的文档。它是否修改了作为第二个参数传递的时间戳? -
对……它没有(当然)。知道如何解决这个问题吗?
-
我找到了.. 将
strtotime($period, $date)更改为$date = strtotime($period, $date)成功了。谢谢! @不要恐慌