Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading