tags:

views:

28

answers:

1

As of PHP5.3 (I think) it became a requirement for the date.timezone to be set in the php.ini file, or to be set at runtime via the date_default_timezone_set() function. When you don't set this value you get, an error, something along the lines of this:

PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-4.0/DST' instead in FILE on line LINE.

I was wondering what mechanism it uses to guess the timezone, as it seems to be pretty accurate, and I wanted to know for myself so I can present this error in a more friendly way to the user, or allow them to accept this as the correct timezone.

+2  A: 

I'd probably use the date_default_timezone_get() function:

string date_default_timezone_get  ( void  )

In order of preference, this function returns the default timezone by:

  • 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)

http://www.php.net/manual/en/function.date-default-timezone-get.php

Mike Caron
I feel like such a douché.
Mark Tomlin
That's okay. Everyone has brain farts, just look at my question list! ;)
Mike Caron