strtotime的版本差异("上个月的第一天")?

geo*_*eoB 9 php date

在包含的脚本中

date('Y-m-d', strtotime('first day of last month'))
Run Code Online (Sandbox Code Playgroud)

在版本5.3.10(localhost)中,我得到了例如'2012-03-01'.

在版本5.2.17(远程主机)中我得到'1969-12-31'.

是否有表达式会返回两个版本的预期结果(例如,'2012-03-01')?

Law*_*one 8

你应该使用mktime()函数:

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

See In Action

  • 上个月是12月,这个假期不会吗? (2认同)

Ole*_*leg 5

date('Y-m-d', strtotime('first day of -1 month'))
Run Code Online (Sandbox Code Playgroud)

在 PHP 7.0 上运行良好