0

Been trying to figure out just how to add hours, days, weeks and years to a date. Found a few examples that work, but I have NO idea why.

$dt->add(new DateInterval('P1Y')); 'P1M', 'P1D' all add one year, month and day. 'P1H' or 'P1S' throw exceptions.

Been reading all about DateTime class and reading the https://www.php.net/manual/en/dateinterval.format.php page, NO WHERE can I find anything that explains what the 'P' part of that format string is.

Where is some decent documentation on this??? It should not take hours to figure out how to add a few days to a date!!

Jim D
  • 65
  • 1
  • 8
  • This is why [What does 'P' stand for in the DateInterval format?](https://stackoverflow.com/a/9188794/17087765). `P` is for period and you would do `T` for time. So you want `PT1H` basically. – slashroot Dec 04 '21 at 00:28
  • This has some info about dateinterval: https://www.webfx.com/blog/web-design/php-dateinterval-class/ – Mana S Dec 04 '21 at 00:33
  • It's documented at [DateInterval::__construct()](https://www.php.net/manual/en/dateinterval.construct.php). `__construct()` is the function that gets executed when you use the `new` operator. – Álvaro González Dec 04 '21 at 13:25
  • Thank you all!! Looks like I need to get better at reading technical documentation! – Jim D Dec 04 '21 at 17:51

1 Answers1

0

The P stands for period. If you want to define an interval based on hours or minutes check this example:

$interval = new DateInterval('PT1H');

Here, $interval represents a time interval of 1 hour.

For example, to add an hour to an existing date:

$date = new DateTime();

$date->add($interval);  
Edgar
  • 238
  • 1
  • 7