15

I'm a bit annoyed of keeping PHP's time zone updated. I really love that everything else on my system trusts that I'm maintaining the system's time zone correctly. Maybe there might be use cases where it could be beneficial to configure PHP differently, but why does PHP warn me about relying on my time zone is not safe?

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.

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67

4 Answers4

9

Because Derick said so. That's why. This internals mailing list thread will tell you all about the "logic" behind this decision. The date.timezone warning is a constant annoyance for anyone using PHP as a programming language and not as a replacement for knowing how to write code.

More specifically, this behavior drives me nuts. I wouldn't normally abuse the SO Q&A system to vent but this warning drives me bananas. Also, it's April 1 so why not?

  • Is it really so hard to set `date.timezone` in your `php.ini` file? [Apparently...](http://stackoverflow.com/questions/22760173/setting-timezone-not-applying) – Niet the Dark Absol Apr 01 '14 at 13:05
  • @NiettheDarkAbsol Yes it is! While maintaining system time zone is a no brainer by simply watching a system's clock, a previously configured PHP time zone will easily overseen on time zone change. OK, it's a rare use case, but I'm moving soon my laptop to the 4th time zone. – Markus Malkusch Apr 01 '14 at 13:41
  • @NiettheDarkAbsol It's not a question of difficulty. It's about forcing a known anti-pattern (i.e. registry anti-pattern via .ini files) on users. I have exactly *zero* use for a php.ini file when I'm programming as it's an unnecessary global input to my programs. I shouldn't be forced to use a php.ini to run without unnecessary `E_WARNING` messages. –  Apr 01 '14 at 13:46
8

Here's what PHP devs say about it (in the related discussion):

No - you, as an admin, are required to make an informed decision on what you want your timezone to be. There have been way too many bug reports where people had no clue, so now we throw a warning.

Derick Rethans is the author of this commit that turned date_warning from E_STRICT (in PHP 5.2-) to E_WARNING (PHP 5.3+).

The same discussion has quite a sound (yet obviously awkward) solution to this, applied by MediaWiki:

Actually, the sensible default is what guess_timezone() does already, except without the warnings. You can get that behaviour with e.g.

date_default_timezone_set( @date_default_timezone_get() );

at the top of your program. That's what MediaWiki does (except with by modifying error_reporting instead of using @). We stole the idea from another web app. It's more convenient than duplicating the functionality of guess_timezone() in the application.

It's Derick's prerogative to annoy all users half to death with warnings, as his way of indicating his distaste for the state of OS support for querying of system timezone. That's the reward we give him for writing lots of date/time code.

The key part of this workaround is date_default_timezone_get function, which, in order, 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)
raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

1) using the system Timezone is a major performance overhead.

2) implementations of the TZ database by different vendors have historicaly had lots of functional issues

3) there are issues around the intellectual property of the data. Its the threat of claims and the expense of defending them which undermines such projects - not the validity of the claim. The 2011 case was settled out of court meaning that the matter was only resolved in the case of the plaintiff, not for any future challenges.

4) A consequence of (3) is that even if a system administrator is dilligent with updates there may be upstream issues preventing dissemination of revised data

symcbean
  • 47,736
  • 6
  • 59
  • 94
-2

PHP is often used by people who don't properly maintain their systems, or run on other peoples' systems (shared hosting). If you are properly maintaining your system, then configuring the default PHP timezone separately is simple.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • Your statement "often used by people who don't properly maintain" is based on what? Furthermore I still don't see why it should be worth a warning yelling it is **unsafe**. – Markus Malkusch Apr 01 '14 at 12:30
  • 1
    By the millions of PHP hosting servers, serving tens of millions of PHP driven sites as compared to the number of businesses who can afford to use dedicated resources. – Mihai Stancu Apr 01 '14 at 12:32
  • 1
    My statement is based on over a decade of experience as a professional PHP software developer, working both in large organizations (as I do now) and as an independent contractor (owned my own software development company). I've seen almost every type of people who need "PHP programmers" or "web development". A full 80% of PHP websites that I have seen do not have a proper server admin. – dotancohen Apr 01 '14 at 12:33
  • 2
    How does this answer OPs question? – PeeHaa Apr 01 '14 at 12:48
  • uhm, the default time zone is set initially in the ini file. It's a really different matter then "the lack of maintenance" as you're insisting. At recent PHP installments, the default timezone is set to UTC per default. Either the OP is using old versions or has borked the ini file. – KarelG Apr 01 '14 at 12:51