为什么这段代码不像计划的那样工作?
代码:
$regDate = '2016-10-03';
echo $date1 = date('m', strtotime('+4 month', strtotime($regDate))); echo '<br>';
echo $date2 = date('m', strtotime('+4 month', strtotime($date1))); echo '<br>';
echo $date3 = date('m', strtotime('+4 month', strtotime($date2))); echo '<br>';
echo $date4 = date('m', strtotime('+4 month', strtotime($date3))); echo '<br>';我回来了:
02 05 05
发布于 2016-12-23 12:02:40
它无法确定日期。您需要先计算日期,然后再计算echo日期。试着-
$regDate = '2016-10-03';
$date1 = strtotime('+4 month', strtotime($regDate));
echo date('m', $date1) . '<br>';
$date2 = strtotime('+4 month', $date1);
echo date('m', $date2) . '<br>';
$date3 = strtotime('+4 month',$date2);
echo date('m', $date3) . '<br>';
$date4 = strtotime('+4 month', $date3);
echo date('m', $date4) . '<br>';另一个简单的方法是-
$regDate = strtotime('2016-10-03');
foreach(range(1, 4) as $val) {
$regDate = strtotime('+4 month', $regDate);
echo date('m', $regDate) . '<br>';
}输出
02
06
10
02https://stackoverflow.com/questions/41301018
复制相似问题