首页 PHP 正文
21

strtotime('+1 month')怪异之处

  • yiqingpeng
  • 2019-04-09
  • 0
  •  
使用strtotime函数进行月份上的加减计算时,结果可能并不在意料之中,考察以下代码:
echo date('Y-m-d', strtotime('2019-01-31 +1 month')); //Output: 2019-03-03, 
//而在MySQL中,DATE_ADD( '2019-01-31', INTERVAL 1 MONTH ) 的结果是 2019-02-28
echo date('Y-m-d', strtotime('2019-01-31 +2 month')); //Output: 2019-03-31
echo date('Y-m-d', strtotime('2019-03-31 -1 month')); //Output: 2019-03-03
所以当我们要得到上一个月月份时候,不应该直接使用strtotime('-1 month'), 
而是使用 date('Y-m-01, strtotime(date('Y-m-01') . ' -1 month'));

正在加载评论...