Working with Timezones in PHP


It is very important to pay attention to time zones in PHP. Your server might be hosted in one location while your visitors might be in another time zone. The local time of your visitors will be different from your server’s. To handle such differences, there are several inbuilt PHP functions. These functions allow you to easily tell the timezone of your server, change it and convert them to local date/time.

The first thing that you need to do is figure out the default timezone of your PHP server.

Here’s how:

If you need to change it to your desired timezone, here’s how:

To find the entire list of Timezones in PHP visit this page.

Here’s an example where we use an array containing different Timezones. Those Timezones are set as default with the help of a foreach loop. Then the current time is printed.

Example:

Sometimes it is necessary to calculate the timezone offset. Here is how you do it:

The above example computed the offset of Kathmandu as 20700 seconds from GMT i.e. GMT + 5.75. There’s a whole lot of timezone constructs that are used in Object Oriented PHP programming.

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers