get server time in php - timezone issues
on shell, my server time is (simpledate
in bash):
Sun Aug 29 10:37:12 EDT 2010
when I run the codephp -r "echo date('Y-m-d H:i:s');"
I get: 2010-08-29 10:37:10
but when I execute a php script that hasecho date('Y-m-d H:i:s');
I get a different time- since php thinks the timezone is UTC (server time is EDT).
My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.
Is there a simple way to get the server time- at the timezone that is set for the server?
Answer
Solution:
According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for
, it uses the following order for getting the timezone:
UTC is the default if all of those fail, so that's probably a good starting point.
Answer
Solution:
The server should always have a timezone specified in the
php.ini
file. PHP 5.3 even raises notices if that's not the case and theDateTime
extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with aDateTimezone
object) or having a default one (either throughdate.timezone
ordate_default_timezone_set
).By what you're describing, the most likely scenarios are:
date_default_timezone_set('UTC')
or equivalent.Answer
Solution:
Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:
Answer
Solution:
Have you tried using date_default_timezone_set() American timezones can be found here http://www.php.net/manual/en/timezones.america.php