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

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

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

在使用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,” 的相关文章

干货分享(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 for循环)php跳出循环的语句,php中的几种循环语句,php中的几种循环语句,

免费领取腾讯云服务器! 列举几种php脚本中常用的几种循环语句,比如,for,foreach,do....while 等。 php for循环 for循环在很多语言中都经常用到,比如js语言,c语言,java语言等。...

这都可以(php保存网络图片到本地)php下载图片到本地,php保存图片到本地的方法,php保存图片到本地的方法,

免费领取腾讯云服务器! 下面列几种网上收集的利用php脚本将远程图片保存至本地的方法,各位自行测试代码是否可用,另外php保存远程图片到本地并不能确保百分百的一定会拉取到远程图片的数据。 php保存远程图片到本地的方法 方法...

干货分享(php输出json中文乱码)json数据中文乱码怎么办,解决 php json中文乱码的问题,解决 php json中文乱码的问题,

免费领取腾讯云服务器! php输出使用json_encode函数生成的json数据的时候,发现数据中的中文被转义成了unicode编码,如果使用ajax请求的json数据还好一点,浏览器会自动将unicode编码转义回来,但如果直接输出到浏览器上,那就会直...

速看(js将图片转换为二进制流)js将图片转换为webp,js将图片转换为base64编码方法,js将图片转换为base64编码方法,

免费领取腾讯云服务器! js可以将图片文件转成base64编码的,便于图片文件的上传与处理,下面就写几种关于js脚本来转换图片为base64编码的方法。 js将图片转换为base64编码的方法 方法1 将自身...

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

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

发表评论

访客

看不清,换一张

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