After an hour exploring Stack Overflow, I’m more confused than ever (and based on the number of posts, I’m probably missing something painfully obvious). All I want to do is add n minutes to an existing date/time string (formatted as $start = ‘2022-09-10T09:00:00-07:00’) WITHOUT losing the time zone.
Based on my reading, it looks like I need to use ‘date’ and ‘strtotime’ but when I do I lose the time zone. For example …
$start = date('m:d:y H:i:s', strtotime($start . ' +90 minutes'));
…returns ’09:10:22 14:30:00′
When I try adding formatting like this to get the exact format I’m looking for…
$start = date(DATE_W3C, strtotime($start . ' +90 minutes'));
…it returns the correct format but changes the time zone: ‘2022-09-10T14:30:00+00:00’
All I am trying to do is to take ‘2022-09-10T09:00:00-07:00’ and add 90 minutes to make it ‘2022-09-10T10:30:00-07:00’. What am I missing?
Although there’s nothing wrong with the
strtotime functions, they force you to deal with strings and ints which makes things muddy. Instead, if you can elevate that to a higher level thing, specifically the
DateTimeImmutable) you can reason about things easier.
$date = new DateTime('2022-09-10T09:00:00-07:00'); $date->modify('+5 minutes'); echo $date->format(DateTime::RFC3339); // Result: 2022-09-10T09:05:00-07:00
Demo here: https://3v4l.org/FpQlf