Monday, January 11, 2010

How to set timezone manually for php

Recently, i was trying to fetch some data which had date in it, using XAMPP 1.7.2 and FlashBuilder 4 , and I faced the error given below.

InvocationTargetException:There was an error while invoking the operation. Check your server settings and try invoking the operation again.
Reason: Server error DateTime::__construct() [
datetime.--construct]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Calcutta' for '5.5/no DST' instead #0

To overcome this, we have to manually set the timezone. Explanation is given below.
Since PHP 5.1, there has been an extension named 'date' in the PHP core. This is the new implementation of PHP's datetime support. Although it will attempt to guess your system's timezone setting, you should set the timezone manually. You can do this in three ways :
1) in your php.ini using the date.timezone INI directive
2) on your system using the TZ environmental variable
3) from your script using the convenience function date_default_timezone_set()

I chose the step 3 to make things work. Since i reside in India, i added the below line in my php service.
  • date_default_timezone_set('Asia/Calcutta');
For other countries, timezones can be found here
Below links can be useful for further understanding, which i have used as references.