在调用 DateTime 或 DateTimeImmutable 对象上的 modify() 方法时,是否需要加空格?

huangapple go评论116阅读模式
英文:

Are spaces mandatory when calling modify() on a DateTime or DateTimeImmutable object?

问题

关于 modify()文档显示了一些示例,它们总是在数字和单位之间包含一个空格:

$date->modify('+1 day');
$date->modify('+1 month');

这些空格是否是必需的?使用 +1day+1 day 会得到相同的结果吗?

示例:

$now = new DateTimeImmutable();

echo $now->modify('+1day')->format('c');
echo PHP_EOL;
echo $now->modify('+1 day')->format('c');

我在3v4l上尝试过,它给出了相同的日期,所以看起来空格不是必需的,但我对PHP语言如何定义此输入格式感兴趣。支持的日期和时间格式页面没有提供任何详细信息<sup>(1)</sup>。

<sup>(1)</sup>:此问题已修复:https://github.com/php/doc-en/issues/2674#issuecomment-1674040916

英文:

The documentation about modify() shows some examples that always contain a space between the number and its unit:

$date-&gt;modify(&#39;+1 day&#39;);
$date-&gt;modify(&#39;+1 month&#39;);

Are the spaces mandatory? Does the calls with +1day or +1 day give the same results?

Example:

$now = new DateTimeImmutable();

echo $now-&gt;modify(&#39;+1day&#39;)-&gt;format(&#39;c&#39;);
echo PHP_EOL;
echo $now-&gt;modify(&#39;+1 day&#39;)-&gt;format(&#39;c&#39;);

I tried on 3v4l and it gives the same dates, so it looks like spaces are not mandatory, but I'm interested in knowing how this input format is defined by the PHP language. The Supported Date and Time Formats page doesn't give any detail <sup>(1)</sup>.

<sup>(1)</sup>: it has been fixed since: https://github.com/php/doc-en/issues/2674#issuecomment-1674040916

答案1

得分: 4

"The Supported Date and Time Formats"页面没有提供任何详细信息。

您所询问的格式是相对时间格式之一,请查看它们的描述链接,https://www.php.net/manual/en/datetime.formats.relative.php:

格式 描述 示例
number space? (unit | 'week') 处理相对时间项,其中值为数字。 "+5 weeks"、"12 day"、"-7 weekdays"

这里的问号在space?之后表示空格确实是可选的。

英文:

> The Supported Date and Time Formats page doesn't give any detail.

The format you are asking about is one of the relative ones, so follow the link to their description, https://www.php.net/manual/en/datetime.formats.relative.php:

> | Format | Description | Examples |
> |-|-|-|
> | number space? (unit | 'week') | Handles relative time items where the value is a number. | "+5 weeks", "12 day", "-7 weekdays" |

That question mark there after space?, means that the space is indeed optional.

huangapple
  • 本文由 发表于 2023年8月10日 20:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定