当前位置:首页 > 电脑知识 > 正文内容

这都可以(PHP百度百科)pHp是什么,php中关于strtotime函数31日取前几个月日期的BUG,php中关于strtotime函数31日取前几个月日期的BUG,

sauo3年前 (2022-09-17)电脑知识114

在使用php脚本中的 strtotime 函数取前几个月的日期时,发现每到31日时,取出的前几个月的日期都会出现错误。仔细检查了一下,发现在利用 strtotime 函数取前几个月的日期时,给 strtotime 函数的参数并不规范,strtotime 函数的时间划算会出现问题。

strtotime函数取前几月日期的错误复现

示例代码:

PHP
# 飞鸟慕鱼博客 feiniaomy.com // $now = date(Y-m-d); $now = 2022-08-31; //为了复现BUG,这里的日期设置为一个有31号的月份 $time = strtotime($now. -1 month); echo date(Y-m-d,$time); // 2022-07-31 echo <hr>; $time2 = strtotime(date(Y-m-01, strtotime($now. -2 month))); echo date(Y-m-d,$time2); // 2022-07-01 echo <hr>; $time3 = strtotime(date(Y-m-01, strtotime($now. -3 month))); echo date(Y-m-d,$time3); // 2022-05-01 echo <hr>; $time4 = strtotime(date(Y-m-01, strtotime($now. -4 month))); echo date(Y-m-d,$time4); // 2022-05-01 echo <hr>; $time5 = strtotime(date(Y-m-01, strtotime($now. -5 month))); echo date(Y-m-d,$time5); // 2022-03-01 echo <hr>; $time6 = strtotime(date(Y-m-01, strtotime($now. -6 month))); echo date(Y-m-d,$time6); // 2022-03-01复制

PS:

1、通过上面的代码可以看到,利用 strtotime() 函数取上个月,上上个月的日期是从一个指定日期(也可以是当前的日期)进行月份的相减,从而获得想要的日期。

2、如果 strtotime() 函数指定的日期为31号,那么从其基础上减去月份的日期里面如果没有31日,那么 strtotime() 函数就会取一个相近的日期进行输出,这就造成了每到31日时取日期错误的BUG。

strtotime函数取前几月日期正确的方法

示例代码:

PHP
# 飞鸟慕鱼博客 feiniaomy.com // $now = date(Y-m-01); //当前月份的开始时间 $now = 2022-08-01; //调整被减去时间为每月的1号即可。 $time = strtotime($now. -1 month); echo date(Y-m-d,$time); // 2022-07-31 echo <hr>; $time2 = strtotime(date(Y-m-01, strtotime($now. -2 month))); echo date(Y-m-d,$time2); // 2022-06-01 echo <hr>; $time3 = strtotime(date(Y-m-01, strtotime($now. -3 month))); echo date(Y-m-d,$time3); // 2022-05-01 echo <hr>; $time4 = strtotime(date(Y-m-01, strtotime($now. -4 month))); echo date(Y-m-d,$time4); // 2022-04-01 echo <hr>; $time5 = strtotime(date(Y-m-01, strtotime($now. -5 month))); echo date(Y-m-d,$time5); // 2022-03-01 echo <hr>; $time6 = strtotime(date(Y-m-01, strtotime($now. -6 month))); echo date(Y-m-d,$time6); // 2022-02-01复制

修复方法:只需要将 strtotime 处理日期的基础日期调整为指定月份的1日即可,然后获取的日期都是指定月期的1日,再根据自己的需求处理即可!

本文链接:http://blog.sauo.top/?id=273 感谢分享!

分享到:

扫描二维码推送至手机访问。

版权声明:本文由冬眠先生个人博客发布,如需转载请注明出处。

本文链接:http://blog.sauo.top/?id=273

分享给朋友:

“这都可以(PHP百度百科)pHp是什么,php中关于strtotime函数31日取前几个月日期的BUG,php中关于strtotime函数31日取前几个月日期的BUG,” 的相关文章

新鲜出炉(ubuntu系统与centos系统区别)ubuntu还是centos,centos系统与ubuntu系统的区分,centos系统与ubuntu系统的区分,

免费领取腾讯云服务器! Linux的发行版有很多,而我们经常使用的为centos与ubuntu,下面这篇博文就说说这两个系统的发源与区别。 CentOS CentOS(Community Enterprise Operati...

全程干货(php将图片转为对象)php文件怎么转成图片,php将图片转为base64编码格式图片的方法,php将图片转为base64编码格式图片的方法,

免费领取腾讯云服务器! 下面的博文提供两个利用php代码将图片转为base64编码格式图片的方法,分为php转换本地图片为base64图片以及php转换远程图片为base64图片的方法。...

没想到(php获取IP地址)php获取当前时间的函数,php获取顶级域名/一级域名的方法,php获取顶级域名/一级域名的方法,

免费领取腾讯云服务器! 列出几个网上收集的关于利用php脚本获取顶级域名的方法,可能分析的不是很完全,大家可以参考一下,适当的做一些调整。 php获取顶级域名的方法 示例1: Jav...

速看(mac txt文件怎样新建)mac新建一个txt文件,Mac 新建TXT文件的方法,Mac 新建TXT文件的方法,

速看(mac txt文件怎样新建)mac新建一个txt文件,Mac 新建TXT文件的方法,Mac 新建TXT文件的方法,

免费领取腾讯云服务器! 有个刚用mac电脑的同事来问了一个很基础的问题,如何在mac电脑上创建txt纯文本文件,就告诉他了一种不借助第三方软件的方法,下面就来分享一下。...

这都可以(php获取当前时间)php获取月份,php获取上个月,上上个月的开始时间与结束时间的方法,php获取上个月,上上个月的开始时间与结束时间的方法,

免费领取腾讯云服务器! 记录一下利用php脚本获取上个月,上上个月的开始时间与结束时间的方法,下面的示例代码,有需要的可以参考一下。 php 获取上个月,上上个月的开始时...

居然可以这样(js closest方法)什么是closest方法,jquery中 closest() 方法的详解和使用,jquery中 closest() 方法的详解和使用,

免费领取腾讯云服务器! 写在一个JS动态效果的时候,发现了一个在JQuery中从来没有见过的 closest() 方法,看了一下介绍和使用方法,发现非常的好用。下面的博文,飞鸟慕鱼博客就来详细的说一下。 jQuery closest() 方...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。