Date & Time in PHP
Work with dates and times in PHP using date(), time(), strtotime(), and the DateTime class for formatting, comparison, and manipulation.
Introduction to Date & Time
PHP offers two approaches to working with dates: the procedural functions (`date()`, `time()`, `strtotime()`) and the object-oriented `DateTime` / `DateTimeImmutable` classes. The OOP approach is preferred in modern PHP because it is more readable and less error-prone.
1. What is a Date in PHP?
PHP represents dates as Unix timestamps (seconds since 1 Jan 1970 UTC).
Creating Dates
The `DateTime` and `DateTimeImmutable` classes are the modern way to work with dates in PHP. `DateTimeImmutable` is preferred — it never modifies itself; every operation returns a new object.
1. Current Date & Time
`new DateTime()` with no arguments creates an object for the current moment.
2. Date from String
Parse a date string using `new DateTimeImmutable($string)` or `strtotime()`.
3. Date from Components — setDate() / mktime()
Build a date from individual year, month, day values.
4. Date from Custom Format — createFromFormat()
Parse a date string that uses a non-standard format.
Timestamps
A Unix timestamp is the number of seconds elapsed since 1 January 1970 00:00:00 UTC. PHP uses it as the base unit for all date calculations.
1. Getting the Current Timestamp
`time()` returns the current Unix timestamp in seconds.
2. DateTime to Timestamp
`->getTimestamp()` converts a DateTime object to a Unix timestamp.
Getting Date Components
Use format characters to extract individual date components, or the `getdate()` / `date_parse()` functions for arrays of components.
1. Extracting Components via format()
Use single-character format codes to get individual parts.
2. getdate() — Component Array
`getdate()` returns an associative array of all date components.
Formatting Dates
PHP's `date()` function and `DateTime::format()` use the same format characters to produce output in any style — ISO 8601, human-readable, locale-aware, or custom.
1. Common Format Patterns
Format a date into ISO, human-readable, or custom strings.
2. Locale-Aware Formatting — IntlDateFormatter
Format dates in the user's locale using the `intl` extension.
Comparing Dates
`DateTimeImmutable` objects support direct comparison with `<`, `>`, `==`, and `<=>` operators. You can also compare via timestamps for arithmetic precision.
1. Comparing Two Dates
Use `<` / `>` directly on DateTime objects, or compare timestamps.
Date Arithmetic
PHP's `DateInterval` and the `modify()` / `add()` / `sub()` methods on `DateTimeImmutable` make date math readable and precise. The `diff()` method calculates the exact difference between two dates.
1. Adding & Subtracting — modify() and add()/sub()
Use `modify()` with English strings or `DateInterval` for precise intervals.
2. Difference Between Two Dates — diff()
`diff()` returns a `DateInterval` describing the gap between two dates.
3. Procedural Date Arithmetic — strtotime()
Use `strtotime()` for quick timestamp-based date math.
Timezones & UTC
PHP supports named timezones via the `DateTimeZone` class. Always store dates in UTC on the backend and convert to the user's local timezone only for display.
1. Local Time vs UTC
Create dates in a specific timezone using `DateTimeZone`.
2. Converting Between Timezones
Use `setTimezone()` to convert an existing date to another timezone.
Date & Time Best Practices
Date and time bugs are among the hardest to track down. Following a few simple rules eliminates most of them.
1. Recommended Practices
Use DateTimeImmutable, store UTC, and set a default timezone.