PHP 遇到的坑 发布于 2024 年 9 月 16 日 全文约 207 字 · 预计阅读 1 分钟 ### strtotime() 函数在31号时间转换问题 当前时间如果是8月31号,echo date("Y-m-d", strtotime("+1 month")),则会输出 10月1号,并没有输出我们想要的 9月,我们使用 last day of 或者 first day of 修正语句。 #### 修正语句: - last day of 指定月份的最后一天 - first day of 指定月份的第一天  ``` // 如果当前时间是 2022-08-31 //使用 strtotime('+1 month') 来获取下个月 1号会出问题 echo date('Y-m-01', strtotime('+1 month', strtotime('2022-08-31'))); // 输出 2022-10-01,不是我们想要的 2022-09-01 //使用 first day of 来获取下个月 1号 echo date('Y-m-d', strtotime('first day of +1 month', strtotime('2022-08-31'))); // 输出 2022-09-01 // 使用 last day of 获取上个月 最后一天 echo date('Y-m-d', strtotime('last day of -1 month', strtotime('2022-08-31'))); // 输出 2022-07-31 ``` PHP 遇到的坑 https://blog.gamehomexx.asia/index.php/archives/55/ 无责任编辑 joewang 发布时间 2024 年 9 月 16 日 最后编辑时间 2024 年 9 月 16 日 版权协议 CC BY-NC-SA 4.0 php 广告 广告位招租 联系站长投放广告 了解更多 → 分享 文章目录 × PHP 遇到的坑 ← 上一篇Apache Bench(AB) 并发测试 下一篇 →php 的犄角旮旯 发表评论 取消回复 提交评论 评论 暂无评论
### strtotime() 函数在31号时间转换问题 当前时间如果是8月31号,echo date("Y-m-d", strtotime("+1 month")),则会输出 10月1号,并没有输出我们想要的 9月,我们使用 last day of 或者 first day of 修正语句。 #### 修正语句: - last day of 指定月份的最后一天 - first day of 指定月份的第一天  ``` // 如果当前时间是 2022-08-31 //使用 strtotime('+1 month') 来获取下个月 1号会出问题 echo date('Y-m-01', strtotime('+1 month', strtotime('2022-08-31'))); // 输出 2022-10-01,不是我们想要的 2022-09-01 //使用 first day of 来获取下个月 1号 echo date('Y-m-d', strtotime('first day of +1 month', strtotime('2022-08-31'))); // 输出 2022-09-01 // 使用 last day of 获取上个月 最后一天 echo date('Y-m-d', strtotime('last day of -1 month', strtotime('2022-08-31'))); // 输出 2022-07-31 ```
评论 暂无评论