Development
How to Work With Dates and Time Zones in PHP

Work with dates and time zones in PHP by separating an instant from the local clock used to display it. Use DateTimeImmutable, parse expected formats strictly, and make the timezone explicit at input and output boundaries.
A stored instant answers “when did this happen?” A recurring instruction such as “every Monday at 9 AM in Manila” also needs a local timezone and recurrence rule. Those are different kinds of data.
Parse PHP dates and time zones explicitly
This PHP 8.1+ example accepts a local date and time in Manila, rejects normalized invalid dates, and produces an offset-bearing UTC timestamp:
<?php
declare(strict_types=1);
/** Parses one exact local date-time format in a specified zone. */
function parseLocalTime(string $input, DateTimeZone $zone): DateTimeImmutable
{
$date = DateTimeImmutable::createFromFormat('!Y-m-d H:i', $input, $zone);
$errors = DateTimeImmutable::getLastErrors();
if ($date === false
|| ($errors !== false && ($errors['warning_count'] || $errors['error_count']))
|| $date->format('Y-m-d H:i') !== $input
) {
throw new InvalidArgumentException('Use a valid YYYY-MM-DD HH:MM value.');
}
return $date;
}
$local = parseLocalTime('2026-12-01 09:00', new DateTimeZone('Asia/Manila'));
$utc = $local->setTimezone(new DateTimeZone('UTC'));
echo $utc->format(DateTimeInterface::ATOM) . PHP_EOL;
echo $local->format(DateTimeInterface::ATOM) . PHP_EOL;
The expected lines are 2026-12-01T01:00:00+00:00 and 2026-12-01T09:00:00+08:00. Changing the timezone changes the representation, not the instant.
The createFromFormat reference documents format parsing and normalization. getLastErrors() can return false when there are no warnings or errors, so the example handles both forms.
Reject impossible dates instead of accepting normalization
Try 2026-02-30 09:00. A parser that silently normalizes it into March is unsuitable for a form promising to accept a specific calendar date. The warning and round-trip checks reject it.
The leading ! resets unspecified fields. Otherwise a date parsed without seconds can inherit values you did not intend. Keeping parsing in one function makes that policy visible to every caller.
This function does not resolve ambiguous daylight-saving times. In a zone where the clock repeats an hour, the same local string can refer to two instants. Ask for an explicit offset or provide an explicit first/second occurrence selection and validate it against the selected zone. A nonexistent spring-forward time should also be rejected or handled by a documented business rule.
Store the value that matches the business meaning
For an event that already happened, store a UTC instant and retain the originating zone when it matters for display or audit. Be explicit about the precision and database column semantics.
For a birthday or accounting date, a date-only value can be correct. Converting a birthday to midnight UTC can change its displayed calendar day for a user elsewhere.
For a recurring appointment, preserve the local time and IANA timezone identifier. Adding 86,400 seconds means elapsed time; it does not universally mean the same local clock time tomorrow. See PHP's date and time arithmetic examples.
Test the boundaries that change the answer
Run the example with its expected timestamp, an impossible date, a trailing character, and a missing leading zero. Add a zone with daylight-saving transitions if the application serves one. Verify how your database driver writes and reads the resulting value.
Use DateTimeImmutable to make transformations explicit: setTimezone() returns another object, leaving the original available. Avoid hidden dependence on the server's default timezone in code that handles customer schedules.
The same discipline applies to software delivery forecasts: a date needs its assumptions and meaning attached to it.
Should an API return a timezone offset?
For an instant, return an unambiguous representation such as an ISO 8601 timestamp with Z or a numeric offset. If callers need future local scheduling behavior, return the timezone identifier separately as well.
If inconsistent date handling affects billing, access, or scheduled work, review those boundaries together.