活到老学到老  

记录遇到问题的点点滴滴。

strtotime 疑问详解

8年前发布  · 808 次阅读
当前时间是: 2016-03-31

以下经常有人认为是strtotime的bug
echo date('Y-m', strtotime('+1 month')); // 输出2016-05
echo date('Y-m', strtotime('+2 month')); // 输出2016-05

下方是计算过程
<?php
// 当前日期 3-31
// +1 month 等同 +31 day
echo date('Y-m-d H:i:s', strtotime('+1 month'));
echo "\n" . date('Y-m-d H:i:s', strtotime('+31 day'));

echo "\n---------\n";
// 当前日期 3-31
// 下一个月最后一天是 4-30
// +2 month 等同 +31+30 day

echo date('Y-m-d H:i:s', strtotime('+2 month'));
echo "\n" . date('Y-m-d H:i:s', strtotime('+61 day'));

echo "\n---------\n";
// 当前日期 3-31
// 下一个月最后一天是 4-30
// 下下一个月最后一天是 5-31
// +3 month 等同 +31+30+31 day

echo date('Y-m-d H:i:s', strtotime('+3 month'));
echo "\n" . date('Y-m-d H:i:s', strtotime('+92 day'));

// 输出结果
//2016-05-01 16:25:01
//2016-05-01 16:25:01
//---------
//2016-05-31 16:25:01
//2016-05-31 16:25:01
//---------
//2016-07-01 16:25:01
//2016-07-01 16:25:01