How does php manage different time zones?

If you like to solve this issue using PHP, check this out.
The following code can be used to find the local time and the Greenwich Mean Time(GMT).


Use these steps to show the time based on location.

  1. Add a Session variable which stores the difference of local and GMT time.
  2. Add the difference to the GMT time before showing in the site.

For example,

# Here shows the difference 
$diff = (strtotime(gmdate("M d Y H:i:s",time())) - strtotime(date("M d Y H:i:s",time())));  

echo '
'.(date("M d Y H:i:s",time())); # Oct 16 2013 08:09:23 echo '
'.gmdate("M d Y H:i:s",(time()-$diff)); # Oct 16 2013 08:09:23

I'm working on a web application where there will be many different users from all over the world making updates. I'm wondering what the best way to handle timezones would be? Ideally, as an event happens, all users view it using their local times.

Should the server OS time be set to GMT, physical location time, or my development location time?

Should the application logic (PHP) and database (MySQL) be set to store data as GMT or local time (local to users)?

Is there an industry standard or even a simple/obvious solution that I'm just not seeing?

asked Feb 24, 2011 at 17:33

0

Save your events in MySQL's TIMESTAMP format - it is stored internally as UTC regardless of the server / user timezone. This way the data is portable regardless of your server's specific configuration. If you store it in any specific timezone, you will have much more work converting it into different timezones.

Fetch it from the database into a numeric timestamp (using the UNIX_TIMESTAMP() function) for use with PHP's various date/time functions (such as date() ).

You then need to set for each user the PHP timezone using date_default_timezone_set() - you can get that information either via user configurable settings or from the browser headers (less accurate). You can then use PHP date/time functions as you would normally, and the output will be in the user's timezone.

Another alternative is to show relative time (for example: "5 hours and 2 minutes ago").

answered Feb 24, 2011 at 17:39

How does php manage different time zones?

1

I would use the UTC time in the database, and have the clients convert that to their local time when needed

answered Feb 24, 2011 at 17:36

RachelRachel

23.9k16 gold badges90 silver badges157 bronze badges

3

In our world, we have different timezones. Each timezone follows a uniform standard time for the countries and their subdivision which falls under a timezone.

As a developer, sometimes you need to deal with different timezones. You may need to execute a code on the basis of the date and time of specific timezones. PHP provides a few classes and functions that help to handle timezones in the application. Let’s see how to handle timezones in PHP.

Get Default Timezone of Server

PHP websites run on a server. Each server has its default timezone. If you want to know the timezone of your server, use the code below.

One can also check the server’s timezone in the php.ini file. Open the file and search for date.timezone and you will get the timezone. In my case, I can see the timezone set to Asia/Kolkata.

date.timezone=Asia/Kolkata

When you print the date and time, you will get it as per the timezone of your server. For me, it will be local time in India.

Change Default Timezone of Your Server

If someone wants to change the default timezone of the server, use the method date_default_timezone_set() and pass the timezone string to this function. You can get a list of available timezones here.

For instance, I set ‘Pacific/Nauru’ as the default timezone.

Alternatively, you can also set the timezone into the php.ini file. Open it, search for date.timezone and assign value to it as follows.

date.timezone=Pacific/Nauru

When you change the php.ini settings, you need to restart the apache server to reflect the new changes.

Now if you print the date and time again, you will get it as per the ‘Pacific/Nauru’ timezone.

How to Handle Timezones in PHP On The Fly

There might be some scenarios where you want to get dates and times from different time zones on the fly. You can’t set or use the default timezone in such cases. To deal with those scenarios, use the DateTime class provided in PHP.

Let’s say you want to get the current date and time of the timezone ‘America/Los_Angeles’. For this, you can use the below code.

setTimezone($otherTZ);
echo $datetime->format('Y-m-d H:i:s');

The above code sets the timezone ‘America/Los_Angeles’ at runtime and gives you the date and time accordingly.

This is how one can handle timezones in PHP. I hope you understand the topic. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

  • Find Geolocation using IP Address for Free in PHP
  • Compress Images for Website using reSmush.it in PHP
  • How to Work with PHP DateTime using Carbon

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

How do you manage time zone differences?

Seven Tips for Dealing with a Time Zone Difference.
Utilize online tools. ... .
Limit yourself to one time zone. ... .
Communication is key. ... .
Know your limits and set expectations. ... .
Allocate enough time for sleep. ... .
Use the time difference to your advantage. ... .
Don't forget about daylight saving time..

How can we convert the time zones using PHP?

It's really simple to convert a DateTime from one time zone to another in PHP. Just create a DateTime object using date & time to be converted as the first parameter and the original time zone as the second parameter. Then change the time zone to the desired one using the setTimezone method. That's all!

Where is PHP timezone set?

8 Answers.
Go to your phpinfo() page and search for Loaded Configuration File and open the php. ini file mentioned under that section..
Change the default timezone settings by adding your new timezone by modifying this line: date. timezone=Asia/Kolkata ..
Save the php. ... .
Restart the Apache server..

What is PHP default timezone?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.