Published on

Formatting Dates and Times in PHP: Displaying Time as "GMT +8"

Authors

When working with PHP’s DateTime class, a common issue developers encounter is formatting the timezone offset to display in the “GMT +X” format, such as "GMT +8" instead of the default "+08:00". In this guide, we’ll explore how you can achieve this and adjust the output to match your desired format.

Why Timezone Formatting Is Important

Whether you're developing an international app or simply need to ensure your app displays the correct local time, managing timezones in PHP is crucial. By default, PHP provides the timezone offset in a ±HH:MM format, which works fine in most cases. But sometimes, you might want to display the offset as GMT +X, especially for better readability or specific requirements.

This article will take you step-by-step through the process of customizing timezone formatting in PHP.

Understanding PHP's Timezone Handling

Before diving into the solution, it's helpful to understand how PHP handles timezones.

PHP provides a variety of functions for working with timezones, but the most flexible and powerful approach is using the DateTime class with DateTimeZone. This allows you to specify a timezone and retrieve the current time in that zone.

Here’s a simple example of how to display the current time in Singapore:

$currentTime = new DateTime("now", new DateTimeZone("Asia/Singapore"));
echo $currentTime->format('g:i:s T P');

This outputs something like:

3:04:38 +08 +08:00

As you can see, by default, PHP shows the timezone abbreviation (e.g., +08) and the full timezone offset (e.g., +08:00), but we want it in the format GMT +8.

How to Display Time in "GMT +X" Format

To achieve the desired format, we can use DateTime but customize the output by manipulating the timezone offset. Specifically, we need to strip the :00 from the offset and append "GMT" to it.

Here’s how to do it:

$currentTime = new DateTime("now", new DateTimeZone("Asia/Singapore"));
$formattedTime = $currentTime->format('g:i:s'); // Format the time
$gmtOffset = $currentTime->format('P'); // Get the GMT offset like +08:00
$gmtHour = str_replace(':00', '', $gmtOffset); // Remove the :00 to get +8

echo $formattedTime . " GMT " . $gmtHour;

Output:

2:57:35 GMT +8

Now you have the time formatted in the "GMT +8" style!

Breaking Down the Solution:

  1. Retrieve the current time using DateTime.
  2. Format the time to your preferred format (e.g., g:i:s for 2:57:35).
  3. Extract the timezone offset using P (which gives the full +08:00).
  4. Remove the unnecessary :00 from the offset using str_replace().
  5. Concatenate "GMT" and the simplified offset for the final result.

Why This Matters for User Experience

Timezone formatting might seem like a minor detail, but when users from different regions access your app, they expect the time to be clear and understandable. Displaying the offset as GMT +X is often more recognizable and easier for users to process than the default +08:00.

Conclusion

Working with timezones in PHP can be tricky, but with the right approach, you can customize the output to suit your needs. By using the DateTime class and some simple string manipulation, you can easily display time in a "GMT +X" format, enhancing both the clarity and usability of your application.

For more coding tips and tutorials, check out other articles on NC's Blog.