on shell, my server time is (simple date
in bash):
Sun Aug 29 10:37:12 EDT 2010
when I run the code php -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 has echo 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?
The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.
PHP has a function for it using GeoIP. The geoip_time_zone_by_country_and_region() function will return the time zone corresponding to a country and region code combo.
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 date_default_timezone_get
, it uses the following order for getting the timezone:
* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)
UTC is the default if all of those fail, so that's probably a good starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With