你真的了解strtotime('X month')吗
经常会有人被strtotime结合-1 month, +1 month, next month的时候搞得很困惑, 然后就会觉得这个函数有点不那么靠谱, 动不动就出问题. 用的时候就会很慌…
今天是2018-08-31 执行代码:
date("Y-m-d",strtotime("+1 month"))
怎么输出是2018-10-01?
虽然这个问题看起来很迷惑, 但从内部逻辑上来说呢, 其实是”对”的
date内部的对于这种事情的处理逻辑:
- 先做-1 month, 那么当前是07-31, 减去一以后就是06-31.
- 再做日期规范化, 因为6月没有31号, 所以就好像2点60等于3点一样, 6月31就等于了7月1
是不是逻辑很”清晰”呢? 我们也可以手动验证第二个步骤, 比如:
var_dump(date("Y-m-d", strtotime("2017-06-31")));//输出2017-07-01
也就是说, 只要涉及到大小月的最后一天, 都可能会有这个迷惑, 我们也可以很轻松的验证类似的其他月份, 印证这个结论:
var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2017-03-31"))));//输出2017-03-03var_dump(date("Y-m-d", strtotime("+1 month", strtotime("2017-08-31"))));//输出2017-10-01var_dump(date("Y-m-d", strtotime("next month", strtotime("2017-01-31"))));//输出2017-03-03var_dump(date("Y-m-d", strtotime("last month", strtotime("2017-03-31"))));//输出2017-03-03
那怎么办呢?
从PHP5.3开始呢, date新增了一系列修正短语, 来明确这个问题, 那就是”first day of” 和 “last day of”, 也就是你可以限定好不要让date自动”规范化”:
var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));//输出2017-02-28var_dump(date("Y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31"))));////输出2017-09-01var_dump(date("Y-m-d", strtotime("first day of next month", strtotime("2017-01-31"))));////输出2017-02-01var_dump(date("Y-m-d", strtotime("last day of last month", strtotime("2017-03-31"))));////输出2017-02-28
那如果是5.3之前的版本(还有人用么?), 你可以使用mktime之类的, 把所有的日子忽略掉, 比如都限定为每月1号就可以了, 只不过就不如直接用first day来的更加优雅.
有人将这个问题提交为bug ,https://bugs.php.net/bug.php?id=22486
转自: http://www.laruence.com/2018/07/31/3207.html
你真的了解strtotime('X month')吗的更多相关文章
- php: +1天, +3个月, strtotime(): +1 day, +3 month
php: +1天, +3个月, strtotime(): +1 day, +3 month 比如,我现在当前时间基础上+1天: strtotime("+1 day"); 比如我现 ...
- php函数强大的 strtotime
使用strtotime可以将各种格式的时间字符串转换为时间戳 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 转换常规时间格式 echo date('Y-m-d H:i: ...
- 时间函数 date strtotime
date_default_timezone_set('Asia/Shanghai');echo strtotime('Today');echo strtotime(date('Y-m-d')); 获取 ...
- php strtotime 和 date 日期操作
time()得到的数值是1970-1-1到现在的秒数,strtotime("now")也是,两个值是相同的. http://doc.thinkphp.cn/manual/syste ...
- PHP中的strtotime()对于31日求上个月有问题
原文出处 <?php $date = "2012-07-31"; $date_unix = strtotime($date); $lastmonth = strtotime( ...
- 谨慎使用php的strtotime()函数
我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题.例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime( ...
- strtotime的几种用法区别
strtotime不仅可以使用类似Y-m-d此类标准的时间/日期字符串来转化时间戳, 还可以用类似自然语言的来生成时间戳, 类似: strtotime('last day'); strtotime(' ...
- strtotime 获取之前,之后时间
一年之前 <?php echo strtotime('-1 year'); ?> 一年之后 <?php echo strtotime('+1 year'); ?> 一月之前 & ...
- [PHP] 重回基础(date函数和strtotime函数)
date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49 使用函数date(),输出当前是月份中的第几天,参数:String类型 d 例如:echo date(&qu ...
随机推荐
- JDBC的安装与使用
JDBC的安装 首先在登录MySQL的官网下载JDBC-MySQL数据库驱动,或者去www.mysql.com/products/connector直接下载. 因为jdbc包属于第三方包,因此要自己导 ...
- 『大 树形dp』
大 Description 滑稽树上滑稽果,滑稽树下你和我,滑稽树前做游戏,滑稽多又多.树上有 n 个节点,它们构成了一棵树,每个节点都有一个滑稽值. 一个大的连通块是指其中最大滑稽值和最小滑稽值之差 ...
- iis7 下配置 ASP.NET MVC 项目遇到的问题 (WIN7 64位 旗舰版 第一次配置站点)
转自 https://www.cnblogs.com/Leo_wl/p/3866625.html,再次感谢 指定的目录或文件在 Web 服务器上不存在. URL 拼写错误. 某个自定义筛选器或模块(如 ...
- MongoDB常用数据库命令第三集
show dbs 查看已经存在的数据库 use 数据库名 切换到指定的数据库(无论数据库是否存在 均可切换成功) db 查看当前数据库 db.getCollectionNames() 查看当前数据库下 ...
- laravel报错 No query results for model . 的解决方法
这个通常由路由绑定出的问题,注意有绑定模型的路由,同路径的路由需要放在没绑定路由的后面 例如:/product/comments和/product的是同路径,/product必须放在/product/ ...
- react-custom-scrollbars的使用
react-custom-scrollbars的作用 流畅的本机浏览器滚动 移动设备的本机滚动条 完全可定制 自动隐藏 自动高度 通用(在客户端和服务器上运行) requestAnimationFra ...
- A dependency may only have one source
在使用Flutter的时候添加依赖报错了 Error on line 21, column 5 of pubspec.yaml: A dependency may only have one sour ...
- 转:oracle 体系结构
前几天面试的时候面试官才问过我ORACLE的体系结构,让我在一张白纸上画出来.回头想想当时答得还不错,大部分内容都描述出来了,呵呵,刚才在网上看到一篇讲解ORACLE体系结构的文章,觉得不错,转过来存 ...
- centos7 ntp server & samba
最近公司内部一个需求:必须 Linux建个 ntp server ,并且 Windows可以net time \\ip 访问. 想要解决问题,还得解决前置问题. 服务器不能上网,无法直接访问外部 yu ...
- Unity整合TortoiseSVN
解决各种漏传 资源 / 代码 的疑难杂症. 因为Unity比较特殊的meta文件系统, 忘传漏传文件在后期可能导致重大引用丢失, 将SVN整合进项目势在必行. TortoiseSVN自带了命令行工具, ...