php - 奇怪的php日期strtotime问题
问题描述
我在 PHP 中使用“date”函数和“strtotime”看到了一个奇怪的问题。
echo date('m/d/Y h:i:s a', time())."<br>"; //returns 04/29/2021 12:26:30 pm
echo date('M Y', strtotime('-5 months'))."<br>"; //returns Nov 2020
echo date('M Y', strtotime('-4 months'))."<br>"; //returns Dec 2020
echo date('M Y', strtotime('-3 months'))."<br>"; //returns Jan 2021
echo date('M Y', strtotime('-2 months'))."<br>"; //returns Mar 2021
echo date('M Y', strtotime('-1 months'))."<br>"; //returns Mar 2021
echo date('M Y')."<br>"; //returns Apr 2021
我的服务器时间是正确的,如第一行所示,但为什么 strtotime('-2 months') 和 strtotime('-1 months') 返回相同的值(2021 年 3 月)两次?
提前致谢
解决方案
您将在 4 月 29 日测试此功能
echo date('M Y', strtotime('-2 months'))
。它将返回 2021 年 2 月 29 日 - 一个不存在的日期,因此它默认为 2 月 (28) 的最后一天并加 1 以获得 3 月 1 日。
...我在这里有一个解决方案,但与您下面的解决方案相比,它有点像黑客,所以我删除了它以支持您的解决方案
推荐阅读
- python - Python Firebase Admin SDK 显示成功,但我从未收到通知
- java - 将 Wildfly 14 双向 SSL 限制为特定客户端
- reactjs - ComponentWillReceiveProps 使用多个同步调度调用一次,但使用异步调度调用多次?
- ruby-on-rails - Webpacker、babel、uglifyjs-webpack-plugin - 不转换箭头函数,但仅在 Vue 文件中
- javascript - 当 h4 包含某些文本时隐藏父 DIV
- angular - Angular 6:接收对象并解析它的问题
- c# - 在微服务中创建域模型和视图模型的正确方法
- javascript - Angular7 外部 Jquery 插件 - 意外的标识符
- r - 检查每个向量元素是否减少 1 或更多的函数
- javascript - Babel 继承丢失原型