Drakes Drakes - 8 months ago 32
Linux Question

How to get the physical server timezone offset (not from php.ini or IP)?

How can one get the timezone offset of the physical machine sitting, not the timezone reported by php.ini (and certainly not through geoIP)?

From php.ini or in scripts we can easily set the default timezone. For example, on my box it's



if (date_default_timezone_get()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';

if (ini_get('date.timezone')) {
echo 'date.timezone: ' . ini_get('date.timezone');

date_default_timezone_set: Asia/Tokyo

date.timezone: Asia/Tokyo

However, the physical server is sitting in Mountain Time with the access logs having the timezone offset string -0700 in them.

Solved: Using
we can get system time and the current timezone offset. This is exactly what was needed. Thanks @codisfy for the inspiration.

echo shell_exec('date +\'%:z\'');


Edit: This question (PHP 5.4 Can't Determine Time Zones on its own) is about getting the timezone name, but of importance here is the timezone offset. Thanks for pointing out a possible duplicate, community, alas it is a different requirement.


From comment to answer: You can get the system time using shell_exec(). With which you can run a command as it would run on a shell and return you the output.

So doing something like this can help:

shell_exec('date +\'%Z\'');