Search

Tuesday, January 25, 2011

Set Default Timezone for PHP requirement (since PHP 5.1)

Since 5.1.0 PHP encouraged to set default time zone by generating WARNING whenever function like date() used while local timezone has not been set.



Warning message will appeared like below :

PHP Warning:  date(): 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/Jakarta' for 'WIT/7.0/no DST' instead in filename.php on line 10

To prevent previous warning message, default timezone needed to be set. Set up statement will affect the whole application, so it might be put in some sort of startup routines in your application. As example below line will set default timezone to "Asia/Jakarta" :
<?php date_default_timezone_set("Asia/Jakarta");
Once has been set. We can use date() function without generating any E_WARNING, such below :
<?php echo date("d m Y");

Reference : http://www.php.net/manual/en/function.date-default-timezone-set.php

No comments:

Post a Comment