PHP strtotime +1 month behaviour

前端 未结 8 2191
再見小時候
再見小時候 2020-11-30 10:10

I know about the unwanted behaviour of PHP\'s function

strtotime

For example, when adding a month (+1

8条回答
  •  执念已碎
    2020-11-30 10:31

    what you need is to tell PHP to be smarter

    $the_date = strtotime('31.01.2011');
    echo date('r', strtotime('last day of next month', $the_date));
    
    $the_date = strtotime('31.03.2011');
    echo date('r', strtotime('last day of next month', $the_date));
    

    assuming you are only interesting on the last day of next month

    reference - http://www.php.net/manual/en/datetime.formats.relative.php

提交回复
热议问题