缩短月份的时间戳php

时间:2015-04-06 22:20:54

标签: php timestamp

我需要一个遍历月份的列表,并始终显示最后几天

2015-04-30
2015-03-31
2015-02-28
...

我的想法是用strtotime方法做到这一点,其中'1430344800'是2015-04-30的时间步长

$time_temp = 1430344800;
echo date('Y-m-t',$time_temp)."<br>";

$time_temp  =   strtotime("-1 month",$time_temp);
echo date('Y-m-t',$time_temp)."<br>";

$time_temp  =   strtotime("-1 month",$time_temp);
echo date('Y-m-t',$time_temp)."<br>";

但我得到了

2015-04-30
2015-03-31
2015-03-31

用'Y-m-d'代替'Y-m-t'给出

2015-04-30
2015-03-30
2015-03-02

为什么不能正确减少月份,我该如何完成呢?

1 个答案:

答案 0 :(得分:0)

$lastDayOfMonth = time(); // depending on what you're trying to do, this could change. 
// For example, it could be = strtotime("+1 month");
for( $i = 0; $i < $numberOfMonthsToShow; $i++ ){
    $lastDayOfMonth = strtotime("last day of previous month", $lastDayOfMonth);
    echo date('Y-n-j', $lastDayOfMonth).'<br />';
}