Version difference for strtotime('first day of last month')?

后端 未结 4 894
一生所求
一生所求 2021-01-02 07:55

In a script that contains

date(\'Y-m-d\', strtotime(\'first day of last month\'))

in version 5.3.10 (localhost) I get, for example, \'2012

相关标签:
4条回答
  • 2021-01-02 08:34
    date('Y-m-d', strtotime('first day of -1 month'))
    

    Works fine on PHP 7.0

    0 讨论(0)
  • 2021-01-02 08:37

    As simple as:

    date('Y-m-01')
    

    The first day of any month always is 1.

    0 讨论(0)
  • 2021-01-02 08:43

    That's a known bug from PHP 5.2.17

    0 讨论(0)
  • 2021-01-02 08:46

    You should use the mktime() function:

    <?php 
    echo date('Y-m-d', mktime(0,0,0,date('n')-1,1,date('Y'))); //2012-03-01
    ?>
    

    See In Action

    0 讨论(0)
提交回复
热议问题