strtotime 获取月份数据不对

项目需要获取前两个月的第一天的时间,开始是这样写的:

// 当前是8月31号
$res = date('Y-m-01', strtotime('-2 month'))
echo $res;  // 结果是 2018-07-01

想不到吧,我也没想到,也不知道是PHP的bug还是我不懂这个的设计。总之,获取某几个月之前或之后的时间。需要这样写:

// 用今天凌晨的时间,注意,不能用 time() 这个是获取当前的时间,
// strototime(date('Y-m')) 也不能带天
$pre_two_month = date('Y-m-01', strtotime('-2 month', strtotime(date('Y-m'))));
echo $pre_two_month;  // 结果是 2018-06-01 正确结果

我获取的是2个月之前的第一天,其他时间请自行修改。

你可能感兴趣的:(PHP)