Skip to content
Home » Web » PHP » How to Resolve "Warning: date(): It is not safe to rely on the system's timezone settings"

How to Resolve "Warning: date(): It is not safe to rely on the system's timezone settings"

Warning: date()

After intalling a newer version of PHP package, you might get this warning message in the client's browser:

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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/html/temp

The above message tells you that you don't have a default timezone, so it chooses UTC for your script instead of unreliable OS timezone, and it suggests you to use date_default_timezone_set() or DateTime object to resolve:

<?php
//Use Timezone Function
date_default_timezone_set("Asia/Tokyo");
echo date("Y-m-d H:i:s");
echo "<br />";

//Use DateTime Object
$date = new DateTime("now", new DateTimeZone("Asia/Tokyo"));
echo $date -> format("Y-m-d H:i:s");
?>

The output contains no warnings this time.

2013-08-30 19:14:43
2013-08-30 19:14:43

But they are not persistent across the script. The best way to resolve it permanently is to modify php.ini as the warning message suggested. First, find date.timezone setting in php.ini:

[root@localhost ~]# vi /etc/php.ini
...
[Date]
...
;date.timezone =
...

And change it into this:

...
[Date]
...
date.timezone = Asia/Tokyo
...

Don't forget to restart httpd.

[root@localhost ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Then, it will be fine to manipulate date without indicating timezone explicitly.

<?php
//Use Timezone Function
echo date("Y-m-d H:i:s");
echo "<br />";

//Use DateTime Object
$date = new DateTime("now");
echo $date -> format("Y-m-d H:i:s");
?>

The code will be less complicated than ever, the output is:

2013-08-30 19:21:21
2013-08-30 19:21:21

Leave a Reply

Your email address will not be published. Required fields are marked *