Warning: date()
After intalling a newer version of PHP package, you might get this warning message in the client's browser:
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
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