嗨,我创建了一个数组,将"25/11/2015"增加1个月。我有一个关于它是如何工作的问题。我当前使用"strtotime('+1 month',$currentdate)"。我想知道为什么在数组的第一行中没有立即添加+1个月。相反,我只剩下11月15日,这正是我想要的,但我一直想知道为什么会发生这种情况。
目前我得到的是:
2015年11月
2015年12月
2016年1月
为什么我不输出这个结果:
2015年12月2016年1月2016年2月
这是我的代码:
$date = "2015-11-25";
$start = strtotime($date);
$currentdate = $start;
$times_table = array();
for($i = 0; $i <= 3; $i++){
$times_table[$i] = array();
}
echo "<pre>";
for($i = 0; $i <= 3; $i++){
for($j = 0; $j <= 4; $j++){
if ($j == 0){
$times_table[$i][$j]= "Version 5" ;
}
else if ($j == 1){
$cur_date = date("M-y", $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
echo $cur_date . ">". "<br />";
}
else{
$times_table[$i][$j]= "gary" ;
}
if ($j == 3) {
$numbers = mt_rand(1, 100);
$times_table[$i][$j]= $numbers ;
}
if ($j == 4){
if($i == 0 || $i == 3)
{
$pay = "P";
$times_table[$i][$j]= $pay ;
}
else{
$int = "I";
$times_table[$i][$j]= $int ;
}
}
}
}
你写的就是你得到的:
$cur_date = date("M-y", $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
$times_table[$i][$j]= $cur_date ;
在添加一个月之前,您将$currentdate值设置为$cur_date,然后存储$cur_dame值。这就是为什么它没有立即添加1个月(它直接添加到$currentdate,但没有添加到$cur_date)