Engineering 4 min read Updated
Storing dates and time zones without surprises
In short
Store moments in UTC, keep the local time and zone for future events, and use immutable PHP date objects with IANA zone names.
Date and time bugs are quiet. A booking shows at the wrong hour for a week, a reminder fires an hour early, or a report shifts a day at midnight. Most come from mixing local times and offsets without saying which is which. Begin with a simple storage rule, then deal with zones and daylight saving time.
Store moments in UTC
The W3C note “Working with Time and Timezones” gives a simple default: when in doubt, use UTC for serialising, storing and exchanging date and time values. Normalising timestamps to UTC also makes data from different sources easier to compare and merge. Convert to the viewer’s zone only when you format a value for display, because the note says the zone belongs at that final step.
When the zone must be kept
Not every value is a moment in time. Opening hours and birthdates are wall-clock values that should stay the same whatever zone the viewer is in. The W3C note calls these floating values. For a future event, such as an appointment at 09:00 in Prague, store the local date, the local time and the zone identifier. Derive the UTC value from them when you need it. Zone rules can change before the event happens, and a UTC value stored in advance may then need updating.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Use immutable objects and IANA names in PHP
PHP offers two classes for dates. DateTime changes the object when you call add or modify, while DateTimeImmutable returns a new object. The PHP manual describes DateTimeImmutable::add as returning a new object with the added interval. Immutable values stop a helper function from changing a timestamp that the caller still uses.
DateTimeZone accepts a zone name, an offset or an abbreviation. Use names from the IANA Time Zone Database, such as Europe/Prague. The PHP manual says the identifier should follow the form published in the IANA time zone database. An abbreviation such as CEST names one fixed offset, so it cannot describe a zone whose rules change. An unknown name throws DateInvalidTimeZoneException, so validate zone names that come from a form before you use them.
Three daylight saving traps
Three situations cause most errors:
- A skipped hour. When clocks move forward, some local times do not exist, so a stored local value in that hour cannot map to a single moment.
- A repeated hour. When clocks move back, one local time occurs twice. Store the offset or the UTC value to tell the two moments apart.
- Arithmetic in hours. Adding 24 hours across a change is not the same as adding one calendar day. Use day-based steps for calendar dates.
Zone data also changes. The IANA database is updated periodically when governments change boundaries, offsets or daylight rules. The PHP manual says its list of timezones is based on a specific database version and that the database can be updated, so keep PHP and the operating system current.
A practical checklist
- Store every timestamp in UTC, and say so in the column name or in the schema documentation.
- For future events, store the local date, the local time and the IANA zone identifier together.
- Use DateTimeImmutable and DateTimeZone in new code, and validate zone names from forms.
- Convert to the viewer’s zone only at the edge, in templates or in responses.
- Write tests for the day the clocks change, one for a skipped hour and one for a repeated hour.
Scheduled jobs are where zone mistakes often hide, so read scheduled jobs that do not fail silently next. Log timestamps in UTC too, as described in logging: what to record and how long.
The rules on storage and display come from the W3C note and the PHP manual. The traps around daylight saving time are general knowledge, not taken from a cited page.
Common questions
- Should every date column hold UTC?
- For moments in time, yes. The W3C note recommends UTC when in doubt for storing and exchanging values. Birthdates and opening hours are wall-clock values and need their own rule.
- How do I store an appointment in the future?
- Store the local date, the local time and the IANA zone identifier. Zone rules can change before the event, so compute the UTC value when you need it rather than once at booking.
- Why does DateTimeZone reject some values?
- An unknown name throws DateInvalidTimeZoneException. Validate the input from forms, and prefer an IANA identifier, such as Europe/Prague, over an abbreviation.
Sources
- Working with Time and Timezones, W3C. Accessed 14 November 2026
- DateTimeZone::__construct, PHP Manual (php.net). Accessed 24 November 2026
- Date and Time, PHP Manual (php.net). Accessed 24 November 2026
- List of Supported Timezones, PHP Manual (php.net). Accessed 24 November 2026
- Time Zone Database, IANA. Accessed 24 November 2026
Further reading
-
Scheduled jobs that do not fail silently
A scheduled job fails silently unless each run is recorded, a missing run raises an alert and a repeated run does no harm.
-
UTF-8 from the editor to the database
Use UTF-8 in every layer, declare it in the HTTP header and the page, name utf8mb4 explicitly in MySQL, and normalise text to NFC.
-
When a database index helps and when it only adds cost
An index helps when a frequent query can use it to find, filter or sort rows, and every write pays for it, so add one only for a query you actually run.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea