PHP strtotime相对时间格式有bug(或功能?)

时间:2015-07-15 07:48:36

标签: php date strtotime

我发现当使用相对时间格式时,strtotime表现得很奇怪。

假设今天是2015年3月31日(通过设置系统时钟完成),当我使用strtotime('-1 month')时,它将返回2015年3月3日。

假设今天是2015年7月31日,当我使用strtotime('-1 month')时,它将返回2015年7月1日,如果今天是2015年8月31日,则返回2015年7月31日。

所以,似乎PHP只是减去1个月,如果该月没有第31天(或2月份的30日和29日),则添加日期。这是PHP的错误吗?或者有没有理由让PHP成为一种功能呢?

最重要的是,无论是错误还是功能,有没有办法可以让它在计算时间时总是返回最后一天? (会有-3个月或其他月份的情况,所以“上个月的最后一天”不行)

1 个答案:

答案 0 :(得分:0)

这样的东西?

<?php
$d = new DateTime( '2010-01-08' );
$d->modify('+1 month');
$d->modify( 'last day of this month' );
echo $d->format('d-M-Y'), "\n";
?>