Overview
PHP's DateTime classes provide object-oriented date and time handling. Prefer explicit time zones and immutable values for predictable application logic.
Common patterns
- Create a value with
new DateTimeImmutable()and an explicitDateTimeZone. - Parse known input formats with
DateTimeImmutable::createFromFormat(). - Format output with
format(). - Compare date-time objects directly when they represent valid instants.
- Use
modify()oradd()to create a changed immutable value. - Convert zones with
setTimezone()without changing the represented instant.
Important distinction
A calendar date such as a birthday is not always equivalent to a UTC timestamp. Model dates according to the business meaning and test daylight-saving transitions where relevant.