r/PHPhelp • u/Wild-Armadillo-8368 • Sep 02 '25
Time offset in DateTime
I noticed that DateTime accepts all time offsets, for example:
DateTimeImmutable::createFromFormat(‘Y-m-d H:i:sP’, ‘2011-02-28 15:04:05+01:23’)
I don't think +01:23 is valid in any time zone.
What would be the best solution to reject such input?
2
Upvotes
View all comments
Show parent comment
1
u/allen_jb Sep 02 '25
Note that brick/date-time (at a glance) does not apply any extra validation in itself to the timezone offset (beyond +/- 18 hours).
I can only assume this has been suggested due to the fact that it breaks down the date/time value into subclasses (and specifically here, breaks down the offset into hours, minutes and seconds).
I would note here that DateTime does provide ->getOffset() that returns the offset in seconds, which is then easy to then validate whether it contains 15-minute-divisible values. eg.
if (($dt->getOffset() % (15 * 60)) === 0)