strtotime可以很方便的计算 1天前 1周前 1月前 1年前的时间戳。 但是月和年的周期是不固定的,使用这个方法会出现异常。 例如 计算3月31日的一个月前的时间结果会怎么样 ?
echo date("Y-m-d", strtotime("-1 month", strtotime("2017-03-31")));
//输出2017-03-03
echo date("Y-m-d", strtotime("+1 month", strtotime("2017-08-31")));
//输出2017-10-01
echo date("Y-m-d", strtotime("next month", strtotime("2017-01-31")));
//输出2017-03-03
echo date("Y-m-d", strtotime("last month", strtotime("2017-03-31")));
//输出2017-03-03
修正短语??first day of 和 last day of? 就是解决这个问题的 ?
echo date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31")));
//输出2017-02-28
echo date("Y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31")));
输出2017-09-01
echo date("Y-m-d", strtotime("first day of next month", strtotime("2017-01-31")));
输出2017-02-01
echo date("Y-m-d", strtotime("last day of last month", strtotime("2017-03-31")));
输出2017-02-28
|