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

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

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

在使用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 防火墙设置)ubuntu防火墙配置文件,ubuntu防火墙的安装与设置,ubuntu防火墙的安装与设置,

免费领取腾讯云服务器! ubuntu系统中的防火墙使用的是iptables,而为了方便防火墙的设置ubuntu提供了一个防火墙管理工具ufw.下面这篇文章就说一说关于防火墙管理工具ufw的安装与使用方法。 ubuntu防火墙ufw的安装与设...

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

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

干货分享(php file_get_contents curl)php file_get_contents post,php file_get_contents(): SSL operation failed with code 1. OpenSSL Error message.....,php file_get_contents(): SSL operation failed with code 1. OpenSSL Error message.....,

免费领取腾讯云服务器! 在调试php脚本代码时,发现使用 file_get_contents() 函数请求HTTPS的网址链接时出现了报错,其报错代码如下面所示“file_get_contents(): SSL operation failed with...

全程干货(查看php是否启动)查看php是否支持rar解压,查看php是ts版本还是nts版本的方法,查看php是ts版本还是nts版本的方法,

全程干货(查看php是否启动)查看php是否支持rar解压,查看php是ts版本还是nts版本的方法,查看php是ts版本还是nts版本的方法,

免费领取腾讯云服务器! 今天有用户来询问php的ts版本与php的nts版本的区别是什么,怎么去查看自己安装的php服务是ts版本还是nts版本,下面博文就来详细的说一下。 php ts版本与nts版本的区别 php官方提供...

一看就会(imagefilter过滤图像方法)php imagefill,php imagefilter()图片滤镜函数的用法与示例,php imagefilter()图片滤镜函数的用法与示例,

一看就会(imagefilter过滤图像方法)php imagefill,php imagefilter()图片滤镜函数的用法与示例,php imagefilter()图片滤镜函数的用法与示例,

免费领取腾讯云服务器! 有个小需求,需要使用php代码对图片进行颜色调整,比如变成灰色,改变图片的亮度,对比度等等。而php中就内置了一个图片的滤镜函数 imagefilter() ,下面就来具体说说这个函数的用法。 php imagefi...

发表评论

访客

看不清,换一张

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