strtotime 踩坑记录

前言

今天是 2019 年的最后一天,即将从 2019 年咸鱼进化成 2020 年咸鱼

就在今早,代码写着写着就出现了一个奇怪的事情:


echo  date('Y/m/d', strtotime('-1 month'));

// 2019/12/01

见鬼了,还能不能好好让我当一条咸鱼了?


echo  date('Y/m/d', strtotime('-2 month'));

// 2019/10/31

唔。。。来看看鸟哥怎么说:令人困惑的 strtotime

date 内部处理逻辑

  • 先做 -1 month :今天是 2019/12/31,上个月的今天就是 2019/11/31

  • 再做日期规范化:因为 11 月没有 31 号,自然成了 12 月的 1 号

验证一下:


echo  date('Y/m/d', strtotime('-1 month', strtotime('2019/03/31')));

// 2019/03/03

那我该如何获取上个月的最后一天日期呢?

相对格式


echo  date('Y/m/d', strtotime('last day of -1 month'));

// 2019/11/30

相对格式

最后

问题解决,继续当回一条可爱的咸鱼。

原文链接:https://learnku.com/articles/38975

发布了84 篇原创文章 · 获赞 33 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_35383263/article/details/103897432
今日推荐