英文:
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->modify('+1 day');
$date->modify('+1 month');
Are the spaces mandatory? Does the calls with +1day
or +1 day
give the same results?
Example:
$now = new DateTimeImmutable();
echo $now->modify('+1day')->format('c');
echo PHP_EOL;
echo $now->modify('+1 day')->format('c');
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论