Technology News

Quick Tip: How To Manage Timezones in PHP

In this quick tip article, we’ll explain the basics of timezone usage in PHP. Working with timezones is an essential skill for every programmer working with web applications. As PHP has been used mostly for web applications throughout the years, it has fairly simple yet comprehensive timezone support.

Timezones and PHP

Timezones have been supported in PHP since version 4. But what started as a basic implementation of the date_default_timezone_set() function rapidly evolved in versions 5 and 7 with the introduction of the DateTimeZone and DateTimeZoneImmutable classes.

The DateTimeZoneImmutable is a subclass of DateTimeZone that makes it possible to create timezones that are immune to modifications. This feature can be very useful in situations where we need to be sure the timezone never changes.

The date_default_timezone_set() function sets the default timezone used by all time/date functions on a given script. This function allows setting up the timezone but has very little granularity, forcing us to use the same timezone during the entire execution of the script.

The DateTimeZone and DateTimeZoneImmutable classes brought more control and versatility to the manipulation of timezones. We can now access all sorts of information on the timezone as well as multiple instances of different timezones on the same script.

Using PHP timezones

As noted, there are two main ways of using timezones in PHP.

Using date_default_timezone_set()

<?php date_default_timezone_set('America/LosAngeles');

Timezones on the date_default_timezone_set() method are always defined in the “Continent/City” or “Continent/Country/City” formats. You can find a complete list of the timezones allowed in PHP here.

This will set the timezone to the timezone passed as an argument. After we’ve set the timezone, we can use functions like date_default_timezone_get() or any other time-related functions such as date() to access the new timezone information.

Using the DateTimeZone class

The better way of using timezones in PHP is by using the DateTimeZone class. This means we have access to more functions to access information and manipulate timezones and we can instantiate multiple instances of the class, which allows us to work with multiple timezones:

<?php $timezone = newDateTimeZone('America/Chicago'); $datetime = new DateTime($date, $timezone); echo $datetime->format('Y-m-d H:i:s');

In this example, we’re creating a new DateTimeZone object, and passing it to a new instance of DateTime. The DateTime object will then use the information from the DateTimeZone object. Finally, we’re using the format() function to output the date in the format we prefer.

Conclusion

As we’ve seen, working with timezones in PHP is fairly straightforward. Make sure you choose the best way of working for your use case, and PHP has got you covered!

10+ Tools & Resources for Web Designers and Agencies (2023 Updated)
Caching Data in SvelteKit

Related Articles

Pinned Audio WordPress Theme

pinned-audio-wordpress-theme
I’m afraid I have to start this with a whole backstory, as the journey here is the point, not so much the theme. A fella wrote to me a while…

Getting Started Guide for Adobe Lightroom

getting-started-guide-for-adobe-lightroom
Even though it is not a requirement for a website to have photos, still, some clients prefer them over graphics as they show the real world. They portray an action…

How To Successfully Sell On Swiggy?

how-to-successfully-sell-on-swiggy
How To Successfully Sell On Swiggy? Places, where the food is being cooked and subsequently savored, are no longer under the same roof and on the contrary, they are increasingly…

Cool Hover Effects That Use CSS Text Shadow

cool-hover-effects-that-use-css-text-shadow
In my last article we saw how CSS background properties allow us to create cool hover effects. This time, we will focus on the CSS text-shadow property to explore even…

20+ Deliciously Dark Website Designs

20-deliciously-dark-website-designs
Dark website designs are where it’s at. Don’t believe me? They’ve become increasingly popular over the past year. Dark designs are thought to be easier on eye strain and make…

What is CIN Number? And How To Get It?

what-is-cin-number-and-how-to-get-it
A Corporate Identification Number (CIN) is a special recognizable proof number that is allocated by the Registrar of Companies (ROC) to the organizations enlisted in India. The ROC gives the…