加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

6月的PHP strtotime将于7月返回

发布时间:2020-12-13 13:23:09 所属栏目:PHP教程 来源:网络整理
导读:我很难过为什么下面的 PHP strtotime函数返回’07’作为月号,而不是$06’当$monthToGet =’June’时: $monthToGet = $_GET['mon'];$monthAsNumber = date('m',strtotime($monthToGet)); 从搜索开始,它似乎可能是由于我没有指定默认日期参数(在这种情况下是
我很难过为什么下面的 PHP strtotime函数返回’07’作为月号,而不是$06’当$monthToGet =’June’时:
$monthToGet = $_GET['mon'];
$monthAsNumber = date('m',strtotime($monthToGet));

从搜索开始,它似乎可能是由于我没有指定默认日期参数(在这种情况下是日期和年份).那会是原因吗?

任何建议赞赏!

TL; DR

你是对的

echo date("m",strtotime("June"));
-> 07

但是,这确实有效:

echo date("m",strtotime("1. June 2012"));
-> 06

问题解释了

今天是2012年7月31日,由于您只提供了一个月,因此当前日期和当前年份用于创建有效日期.

见documentation:

NOTE

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC),relative to the timestamp given in now,or the current time if now is not supplied.

备择方案

您可以使用date_parse_from_format()strptime()以稍微不同的方法实现您想要的效果.

(感谢johannes_和johann__的输入)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读