Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get server time in php - timezone issues

Tags:

timezone

php

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?

like image 474
azv Avatar asked Aug 29 '10 14:08

azv


People also ask

What timezone is my server PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.

How can I get time zone and country code in PHP?

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.


1 Answers

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.

like image 99
Daniel Vandersluis Avatar answered Sep 25 '22 00:09

Daniel Vandersluis