PHP 中使用 strtotime "+1 month" 时发现的坑

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP 中使用 strtotime "+1 month" 时发现的坑脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

strtotime - 将任何字符串的日期时间描述解析为 Unix 时间戳

然后看下这个陨石坑:

  1. echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP: 2009-03-03
  2. echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-31

很明显, 2 月根本没有 30 号, 31 号, 所以上面的 +1 month 直接跳到了三月!

那么, 怎么避免这个问题呢?

  1. echo date( "Y-m-d", strtotime( "First day of next month" ) ); // PHP: 2017-04-01
  2. echo date( "Y-m-d", strtotime( "last day of next month" ) ); // PHP: 2009-04-30

如果你无法肯定日期是否会出现异常, 最好先处理好月份再去处理是哪一天!

: https://blog.csdn.net/lddtime/article/details/66480676

脚本宝典总结

以上是脚本宝典为你收集整理的PHP 中使用 strtotime "+1 month" 时发现的坑全部内容,希望文章能够帮你解决PHP 中使用 strtotime "+1 month" 时发现的坑所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。