php的strtotime(✀+1 month✀),如果是1月31号,应该是2月最后一天,但是得到的却是3月2号,怎么解决这个bug

2025-05-17 07:31:17
推荐回答(3个)
回答1:

需要准备的材料分别是:电脑、php编辑器、浏览器。

1、首先,打开php编辑器,新建php文件,例如:index.php。

2、在index.php中,输入代码:$day = '2019-01-31';$r = date('Y-m-d', strtotime('last day of +1 months', strtotime($day)));echo $r;。

3、浏览器运行index.php页面,此时打印出了2月的最后1天。

回答2:

很简单, 直接上代码吧

//不要当前的DAY, 只要年月
$ym = date('Y-m' , time());
echo  date( 'Y-m-d' , strtotime('+1 month' , strtotime($ym)  ));

回答3:

我在上面的基础上优化了一下

//不要当前的DAY, 只要年月
$ym = date('Y-m', time());
echo date('Y-m-d', strtotime('+2 month -1 day', strtotime($ym)));

---------------------------------------

再次优化

var_dump(date("Y-m-d", strtotime("last day of +1 month", strtotime("2019-01-31"))));
////输出2019-02-28