PHP strtotime()没有返回正确的月份
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP strtotime()没有返回正确的月份,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1282字,纯文字阅读大概需要2分钟。
内容图文
![PHP strtotime()没有返回正确的月份](/upload/InfoBanner/zyjiaocheng/760/2dd73c5d43c34d7ab97007bf5d544780.jpg)
由于当前月/年是2012年1月,为什么以下代码将在2011年12月而不是2011年11月返回?
echo date("F Y", strtotime("-2 months"));
这是在PHP 5.3.0上,如果它有所作为.
解决方法:
要获得您正在寻找的内容,您可以使用这个相当冗长的版本:
echo date("F Y", strtotime("first day of this month - 2 months"));
这里详细介绍了原始版本的问题:http://derickrethans.nl/obtaining-the-next-month-in-php.html.引用如下:
Over and over again PHP users complain that next month in PHP’s
date-string parser doesn’t go to the next month, but instead skips to
the one after next month; like in the following example:06001
The output of the little script will be March. March obviously doesn’t
follow January as February is in between. However, the current
behavior is correct. The following happens internally:next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010, so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at March 3rd. The formatting
strips off the year and day, resulting in the output March. This can
easily be seen when echoing the date with a full date format, which
will output March 3rd, 2010:
这是为了增加月份,但在减去月份时反向相同;没有11月31日,所以strtotime方法“纠正”它到12月1日.
内容总结
以上是互联网集市为您收集整理的PHP strtotime()没有返回正确的月份全部内容,希望文章能够帮你解决PHP strtotime()没有返回正确的月份所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。