jump to navigation

How Do I change Timezone For PHP March 10, 2012

Posted by Tournas Dimitrios in PHP.

PHP provides some nice date manipulation functions that work very well in combination with each other. However, they only handle dealing with the server’s timezone . PHP’s ‘mktime’ and ‘date’ functions work well as a pair without the help of any other timestamp manipulation routines , but only if the application in which they are used is concerned solely with display and entry of time in the servers timezone . If an application needs to handle entry’s from a timezone other than that in which the server is located then PHP has to be configured accordingly . For a permanent timezone configuration  (all visitors get  referenced according the same time-stamp)  open your .htaccess file and add the following line :  SetEnv TZ location , where location is the specific timezone you want to define (Example :  SetEnv TZ  Europe/Athens) . A list of Supported Timezones can be found here >>>>  .

For a per user / visitor timezone configuration use the following line of code at the beginning of the page : putenv(“TZ=Europe/Athens”) ;

You can’t get a user’s timezone on the server side , the simplest way this is usually done is by asking the user (when they register, for instance) what timezone they are in and then use this in your time calculations . If you rely on getting the user’s IP address then you could geolocate that and deduce a timezone ( code example at stackoverflow.com) .


1. ins - March 12, 2012

An outstanding share! I have just forwarded this onto a coworker who had been doing a little research on this.
And he in fact ordered me dinner because I discovered it for him.
.. lol. So let me reword this…. Thank YOU for the meal!
! But yeah, thanx for spending the time to talk about this matter here on your web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s