DEV Community

spO0q
spO0q

Posted on

PHP: Understanding Time Zones

Working with time zones can be tricky in programming.

As PHP is meant for the web, the language supports it. Let's see why you should care.

Why bother with time zones?

If you don't care about time zones, you get bugs sooner or later.

Time-related features may fail (e.g., scheduled events). It gets worse if you have to deal with tricky concepts like Daylight Time.

These concepts are essential for e-Commerce:

  • products availability
  • discounts
  • shipping

Any bug will ultimately hurt the business.

Getting started with time zones in PHP

The most basic but still powerful implementation would be similar to this:

$timeZone = new DateTimeZone('Europe/Berlin');
$date = new DateTime('now', $timeZone);

echo $date->format('Y-m-d H:i:s');
Enter fullscreen mode Exit fullscreen mode

The built-in DateTime class takes a DateTimeZone object as its second argument, which will affect further operations with the $date object.

To find the right time zone, you can use DateTimeZone::listIdentifiers() or this link.

Time diff across time zones

DateTime provides various helpers to run common operations on dates. Time diff is a very basic one, but what happens with different time zones?

$dateTimeNY = new DateTime('2024-08-02 01:01:01', new DateTimeZone('America/New_York'));
$dateTimeBerlin = new DateTime('2024-08-02 07:01:01', new DateTimeZone('Europe/Berlin'));
$interval = $dateTimeNY->diff($dateTimeBerlin);
print_r($interval);// 0 everywhere
Enter fullscreen mode Exit fullscreen mode

Without the time zones 'America/New_York' and 'Europe/Berlin', you would get another diff (6 hours), as PHP would assume $dateTimeNY and $dateTimeBerlin are from the same time zone by default.

It might look obvious here, but if your app does not capture that information correctly, it can have bad consequences.

Use UTC, "always"

While it's possible to set a specific value for the default time zone in PHP, it's usually a better idea to use UTC, especially when you have different time zones to manage.

UTC refers to "the Unix Epoch" and is a universal format.

99.9% of the time, it's recommended to store your dates in UTC.

This way, you will keep it consistent.

Note that you will have to use PHP built-in helpers and converters to manipulate those data, but all stored data will only refer to the same point of origin (00:00:00 UTC on January 1, 1970).

It eases common operations like comparison and sorting. Otherwise, you might end up with values that look the same but are not on the same baseline implicitly.

GMT vs. UTC

GMT means Greenwich Mean Time and used to be the cornerstone of time zones.

Technically speaking, UTC and GMT indicate the same time, but UTC is more accurate (atomic clock), and more consistent (no Daylight Time).

Unlike UTC, GMT is a timezone. You may have seen it in action with each time zone adding or subtracting hours from GMT+0.

These are both time standards for quite similar purposes, but UTC is more sophisticated.

According to various sources, it's abbreviated "UTC" because, otherwise, it would be "CUT" in English ("Coordinated Universal Time") and "TUC" in French ("Temps Universel Coordonné").

What is "Zulu"?

Zulu is a time standard mainly used by military and airline-related stuff.

"Zulu" means "Zero meridian of longitude," which is quite the same as GMT, but the military and the aviators use phonetic alphabet instead of letters.

It's not a critical concept (unlike UTC), but it's good to know, and if you actually need that format, PHP supports it:

Y-m-d\TH:i:sp
Enter fullscreen mode Exit fullscreen mode

Time can change

The concept of "time zones" only regards humans, not machines and programs.

Governments can modify the "time settings." For example, the UTC offset or the observance of DST (Daylight Time) can vary.

Many other events can affect the global time. Another good reason to use the universal format UTC and standardized converters to work with time zones in programming.

Carbon & time zones

Carbon is a set of helpers built on top of the native DateTime object.

As a result, Carbon supports time zones:

$tomorrow = Carbon::now()->tomorrow()->tz('Europe/Berlin');
Enter fullscreen mode Exit fullscreen mode

If you use framework such as Laravel, the Carbon package is already installed.

How to manage Daylight Time

Daylight Time or Daylight Saving Time (DST) is a critical concept for many apps.

In a nutshell, people change the clock to make better use of daylight.

It happens because daylight is usually longer in summer (not everywhere). That's why people advance clocks and set clocks back in the autumn, when the daylight gets shorter again.

In other words, DST is related to sunrise and sunset times, which can be very different around the globe.

You can retrieve those variations by using getTransitions():

$timezone = new DateTimeZone('Europe/Berlin');
$transitions = $timezone->getTransitions();
print_r($transitions);
Enter fullscreen mode Exit fullscreen mode

Known bugs in PHP

If you use the latest version of PHP, you will not get this specific bug, but your case can be a known issue.

Read reported bugs and issues, as it might prevent some headaches.

Useful links

Top comments (2)

Collapse
 
denys_bochko profile image
Denys Bochko

I actually came across this time issue a few years ago and that's when I came to a conclusion that if your clientele is worldwide then it's better to use UTC and then convert to local time for each client.
On the other hand, if your users are in the same time zone, there is no need for UTC, just use your local time.

Great post, though, I wish it would come out when I had this issue :)