Cheat Sheets & Quick References

PHP DateTime and Timezone Cheat Sheet

A concise reference for DateTimeImmutable, DateTimeZone, formatting, parsing and timezone-aware date handling.

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 explicit DateTimeZone.
  • Parse known input formats with DateTimeImmutable::createFromFormat().
  • Format output with format().
  • Compare date-time objects directly when they represent valid instants.
  • Use modify() or add() 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.

Official resource

PHP Date and Time