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