需要准备的材料分别是:电脑、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天。
很简单, 直接上代码吧
//不要当前的DAY, 只要年月
$ym = date('Y-m' , time());
echo date( 'Y-m-d' , strtotime('+1 month' , strtotime($ym) ));
我在上面的基础上优化了一下
//不要当前的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