简体   繁体   English

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

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

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-03-01'. 在版本5.3.10(localhost)中,我得到了例如'2012-03-01'。

in version 5.2.17 (remote host) I get '1969-12-31'. 在版本5.2.17(远程主机)中我得到'1969-12-31'。

Is there an expression that will return expected (eg, '2012-03-01') results for both versions? 是否有表达式会返回两个版本的预期结果(例如,'2012-03-01')?

You should use the mktime() function: 你应该使用mktime()函数:

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

See In Action 见行动

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

适用于PHP 7.0

这是PHP 5.2.17中的一个已知错误

As simple as: 很简单:

date('Y-m-01')

The first day of any month always is 1. 任何月份的第一天总是1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM