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

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

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

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

原创(禁止ping命令操作)linux如何停止ping,linux禁ping操作的方法,linux禁ping操作的方法,

免费领取腾讯云服务器! linux中可以通过ssh命令来禁止其它机器来ping自己,下面是配置方法,大家可以参考一下。 linux中禁ping的方法 方法1 临时禁ping,系统重启后失效,已在ubuntu系...

深度揭秘(Ubuntu 防火墙设置)ubuntu防火墙配置文件,ubuntu防火墙的安装与设置,ubuntu防火墙的安装与设置,

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

干货分享(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获取客户端,php获取服务器操作系统类型的方法,php获取服务器操作系统类型的方法,

免费领取腾讯云服务器! 利用php脚本中的 php_uname() 函数与 PHP_OS 变量可以获取服务器操作系统的类型,具体的使用方法如下。 php获取服务器操作系统的方法 1、php_uname() 获取服务器操作系统...

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

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

这都可以?(jquery去掉前后空格)jquery去掉字符串前后空格,jQuery去掉 serialize() 方法中指定的name值,jQuery去掉 serialize() 方法中指定的name值,

免费领取腾讯云服务器! 在修改一个前台表单提交逻辑时,为了不动以前的代码(代码和屎一样),就想着在提交数据时过滤掉form表单中指定的name属性和值,由于表单直接使用jquery中的serialize()方法来获取的form表单的数据,所以就要对 ser...

发表评论

访客

看不清,换一张

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