How Do I change Timezone For PHP March 10, 2012Posted 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) .