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

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

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

在使用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取消自动休眠)ubuntu关闭自动休眠命令,ubuntu 关闭自动休眠模式的方法,ubuntu 关闭自动休眠模式的方法,

免费领取腾讯云服务器! 在使用ssh连接ubuntu系统时,发现长时间不输入ssh命令ssh就会自动断开。查找了一下原因,发现是安装ubuntu的电脑自动进入了休眠模式,而ubuntu系统中有多种关闭自动休眠的方法,下面就来说一说使用ssh命令来关闭ubu...

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

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

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

这都可以?(常用正则表达式大全)常用正则表达式大全图片,常用正则表达式大全,常用正则表达式大全,

免费领取腾讯云服务器! 网上收集了一些常用的正则表达式,基本上都全,需要的可以拿去了。 关于号码的正则表达式 QQ号正则表达式 BASIC /^[1-9][0-9]{...

难以置信(php错误日志在哪里)PHP 日志,php设置错误日志的方法,php设置错误日志的方法,

难以置信(php错误日志在哪里)PHP 日志,php设置错误日志的方法,php设置错误日志的方法,

免费领取腾讯云服务器! php设置错误日志需要在php配置文件php.ini中设置,只需要简单的几步设置就可以实现,下面就来详细的说一下。 php设置错误日志的方法 1、打开php的配置文件,php.ini...

没想到(imagettftext函数)imagettftext函数支持的编码类型,imagettftext(): any2eucjp(): invalid code in input string 错误解决方法,imagettftext(): any2eucjp(): invalid code in input string 错误解决方法,

免费领取腾讯云服务器! 最近在研究关于php生成图片的东西,发现了 imagettftext() 函数中一个不是错误的错误,具体的错误情况如下所示。 错误代码:...

发表评论

访客

看不清,换一张

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