【发布时间】:2016-07-19 14:15:03
【问题描述】:
我刚刚注意到 PHP 日期函数的行为很奇怪,谁能解释我做错了什么?
以下代码显示相同的结果
<?php
echo date('Y-m-t');
// Outputs last day of this month: 2016-03-31
echo date('Y-m-t', strtotime("-1 month"));
// For some reason outputs the same: 2016-03-31
echo date('Y-m-t', strtotime("+1 month"));
// Outputs 2016-05-31
可能只是我太笨了,但有人能解释一下为什么会这样吗?
【问题讨论】:
-
这一切都记录在 GNU 页面上,PHP 文档也清楚地链接到该页面。 Here:
The fuzz in units can cause problems with relative items. For example, ‘2003-07-31 -1 month’ might evaluate to 2003-07-01, because 2003-06-31 is an invalid date.