All articles

Work with PHP values and data

Display, Check, and Change PHP Dates in Explicit Time Zones

Display PHP dates in UTC, inspect an invalid-date warning, and format a modified date in another time zone without losing the original.

Editorial illustration for Get, Parse, Compare, and Change PHP Dates Across Time Zones

To display the current time and change a date without losing its starting value, name the time zone and keep the result of DateTimeImmutable::modify() in a new variable. If a date arrives as text, inspect parsing warnings before using it. The warning check below answers one specific question about one input; it is not a complete input-validation procedure.

The examples can be placed in one PHP file in the order shown. The first block includes the opening PHP tag. Later blocks continue the same script and reuse $utc and $format.

Get and format the current date and time in UTC

PHP's date() function returns a formatted string. Set the default time zone to UTC before calling it. Then create a DateTimeImmutable for now with an explicit UTC DateTimeZone, so the zone is visible where the object is created.

<?php
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s'), "\n";

$utc = new DateTimeZone('UTC');
$now = new DateTimeImmutable('now', $utc);
echo $now->format('Y-m-d H:i:s'), "\n";

Both lines print the same display pattern, but their digits depend on when each call runs. They can capture different instants, so do not treat matching output as a requirement. The first call uses the default zone set above; the object names UTC explicitly. For object output, PHP's date-format documentation points to DateTimeInterface::format().

A timestamp alone does not specify a display zone. PHP's time() documentation says Unix timestamps contain no local time-zone information and recommends DateTimeImmutable for handling date and time information rather than relying only on timestamps.

Check a supplied date string for a parsing warning

A date-shaped string is not necessarily a valid calendar date. The PHP date-format documentation demonstrates date_parse() reporting a warning for 2015-09-31. Inspect the warnings before proceeding with this supplied value:

$input = '2015-09-31';
$parsed = date_parse($input);

if ($parsed['warnings'] !== []) {
    foreach ($parsed['warnings'] as $warning) {
        echo $warning, "\n";
    }
}

For that exact input, the documented warning is The parsed date was invalid. The code displays warnings found in this parse result; it does not establish that an input is acceptable whenever this particular warning is absent. The fixed date below is a separate example, not a corrected interpretation of September 31.

Modify a fixed date and display it in another time zone

Create the starting value in UTC, assign the result of modify('+1 day') to a different variable, and format both. Keeping $original available matters if later output still needs the starting date.

$original = new DateTimeImmutable('2024-03-15 14:30:45', $utc);
$format = 'Y-m-d H:i:s';

$modified = $original->modify('+1 day');
echo "Original (UTC): ", $original->format($format), "\n";
echo "Modified (UTC): ", $modified->format($format), "\n";
echo "Original again (UTC): ", $original->format($format), "\n";

$amsterdam = new DateTimeZone('Europe/Amsterdam');
echo "Modified (Amsterdam): ",
    $modified->setTimezone($amsterdam)->format($format), "\n";

The UTC lines display 2024-03-15 14:30:45 for $original and 2024-03-16 14:30:45 for $modified. Formatting $original again displays its initial value. The separately labeled Amsterdam line renders the modified value in a named zone; inspect it alongside the UTC rendering rather than hand-editing the clock digits or assuming a particular offset. PHP's date and time documentation describes methods for modification, formatting, and handling time zones.

Choose whether modification should preserve the original object

If changing the object already held by a variable is what you want, DateTime has different modification behavior. The PHP DateTime documentation says its modification methods change the object itself and recommends DateTimeImmutable when you want to avoid that behavior by default.

$mutable = new DateTime('2024-03-15 14:30:45', $utc);
$mutable->modify('+1 day');
echo "Mutable after modify (UTC): ", $mutable->format($format), "\n";

That line displays 2024-03-16 14:30:45 from the same $mutable variable. Choose DateTimeImmutable when later code must still read the starting value without first making a copy; choose DateTime when changing the held object is intentional.

← All articles