strtotime 结果没有意义,php 错误? [英] strtotime result makes no sense, php bug?

查看:31
本文介绍了strtotime 结果没有意义,php 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下行:

echo date('d', strtotime('First Saturday August 2015'));

打印08,这似乎没有任何意义,因为一周中第一次出现的日期不能在第 7 天之后.

prints 08, which doesn't seem to make any sense because the first occurrence of a day of the week can't be after the 7th.

是 php 错误还是 php 错误,甚至是 php 错误?我不知道...

Is it a php bug or a php bug or maybe even a php bug? I don't know...

php 版本:5.5.19

php version: 5.5.19

推荐答案

更新#1:下面解释了像"of",在我对 PHP 源代码进行了一些调查之后.

Update #1: explained below the big difference that a simple word like "of" makes, after I investigated a little in the PHP source code.

更新#2:实际上 PHP 手册上的文档页面解释了strftime() 接受的日期格式.只是直到现在我才意识到这一点.感谢@Outspaced 在他们的回答中提供链接.

Update #2: There actually is a documentation page on PHP manual that explains the formats of dates accepted by strftime(). I just was not aware of it until now. Thanks @Outspaced for providing the link in their answer.

要快速轻松地阅读,请跳过更新 #1 部分.

For a quick and easy reading skip the section Update #1.

确实

echo date('Y-m-d', strtotime('First Saturday August 2015'));

印刷品:

2015-08-08

但是

echo date('Y-m-d', strtotime('First Saturday of August 2015'));

打印正确的日期:

2015-08-01

由于 strtotime() 试图理解"英语,我猜你和它说不同的方言:-)

Since strtotime() tries to "understand" English, I guess you and it speak different dialects :-)

在 PHP 中阅读一些内容后 函数strtotime()的源代码,我认为它将First Friday 2015解析为First Saturday,然后是2015 年 8 月.

After some reading in the PHP source code of function strtotime(), I think that it parses First Saturday August 2015 as First Saturday followed by August 2015.

August 2015 是一个绝对表达式,因为有 没有关于日期的指示 它产生 2015-08-01(2015 年 8 月的第 1st)这是一个合理的猜测(我认为这是唯一可能的).

August 2015 is an absolute expression and because there is no indication about the day it produces 2015-08-01 (the 1st of August 2015) which is a reasonable guess (the only one possible, I think).

第一个星期六相对表达.它相对于字符串中提供的绝对标记(August 2015)或作为 strtotime() 的第二个参数,并且它移位"绝对标记后退或前进一定数量.

First Saturday is a relative expression. It is relative to an absolute mark provided in the string (August 2015) or as a second parameter of strtotime() and it "shifts" the timestamp of the absolute mark back or forward with some amount.

将它们放在一起,strtotime()2015 年 8 月第一个星期六 解释为 第一个星期六8 月 1 日开始计数,2015 年,也就是 2015 年 8 月 8 日.

Putting them together, strtotime() interprets First Saturday August 2015 as First Saturday counting since August 1, 2015 and that is, indeed, August 8, 2015.

另一方面,2015 年 8 月的第一个星期六 使用 不同的规则.nextlastpreviousthisfirst 的任意序号之一到 twelfth,然后是工作日名称(完整或缩写),然后是 of 被解析为所提供月份的工作日(或如果字符串中没有提供当前月份).

On the other hand, First Saturday of August 2015 is parsed using a different rule. One of next, last, previous, this or any ordinal number from first to twelfth, followed by a weekday name (full or abbreviated) and followed by of is parsed as a weekday of the provided month (or the current month if none is provided in the string).

在我发现strtotime()接受的字符串格式后实际上是解释 在 PHP 文档中,解释变得更简单.看看第二个注意:"文档页面中的框.

After I found out that the format of the string accepted by strtotime() is actually explained in the PHP documentation, the explanation became more simple. Take a look at the second "Note:" box in the documentation page.

2015 年 8 月第一个星期六 在注释中的第 4 项中进行了解释:

First Saturday August 2015 is explained at item #4 in the note:

序数日名称"确实提前到另一天.

2015 年 8 月的第一个星期六 在注释的第 6 项中进行了解释:

First Saturday of August 2015 is explained at item #6 of the note:

序数日名称 'of'" 不会前进到另一天.

注释以一整块解释结束,专门针对神奇单词of".

The note ends with an entire block of explanation dedicated to the magical word "of".

这篇关于strtotime 结果没有意义,php 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆