Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP think it's a bad idea to rely on the system's time zone? [duplicate]

Tags:

timezone

php

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.

like image 713
Markus Malkusch Avatar asked Apr 01 '14 12:04

Markus Malkusch


2 Answers

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?

like image 69
rdlowrey Avatar answered Nov 11 '22 23:11

rdlowrey


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)
like image 8
raina77ow Avatar answered Nov 12 '22 00:11

raina77ow