`strtotime` bug when using + 1 month from January

ε祈祈猫儿з 提交于 2019-12-05 16:55:37

You're trying to add one month to the date 2013-01-31. It should give 31th Feburary 2013, but since the date doesn't exist, it moves on to the next valid month (which is March).

You can use the following work-around:

$current = date('n', strtotime("first day of next month",strtotime($start)));

Using DateTime class:

$date = new DateTime('2013-01-31');
$date->modify('first day of next month');
echo $date->format('n');

This will correctly output 2.

Demo!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!