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

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.

Chủ Đề