Google Answers Logo
View Question
 
Q: PHP date() relying on the system's timezone settings ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: PHP date() relying on the system's timezone settings
Category: Computers > Programming
Asked by: vladkornea-ga
List Price: $5.00
Posted: 19 Jun 2006 10:27 PDT
Expires: 19 Jul 2006 10:27 PDT
Question ID: 739376
After increasing PHP's error reporting level on the development server,
I started getting this:

"Strict Standards: date(): It is not safe to rely on the system's
timezone settings. Please use the date.timezone setting, the TZ
environment variable or the date_default_timezone_set() function."

After some research, I'm still not clear on this. This error is
generated by commands such as date('Y'). My understanding is that
date() will simply grab the system date, interpret it, and return
whatever year it contained... and that's it. The server already
contains the date & time information appropriate to its time zone. So
why do I need to set a time zone in PHP? What difference does it make?
Answer  
Subject: Re: PHP date() relying on the system's timezone settings
Answered By: hammer-ga on 19 Jun 2006 11:37 PDT
Rated:4 out of 5 stars
 
Vladkornea,

The E_STRICT setting causes PHP to report errors based on what the PHP
people decided good coding practice to be.  In this case, they seem to
believe that you should be explicit about what time zone you intend to
be in, rather than depending on the server to provide the information.

Part of this has to do with addressing client/server issues, which may
occur when (for example) the server is in Pacific Time and the client
is in Eastern Time. Or there are two servers. One is in Eastern time
and one is in India, but company headquarters is in California and
wants all transactions stamped with their local time regardless of
origin. Which time zone do you want in those cases? Good coding
practice says "Be explicit and specify which time zone you intend."

There is also a bug in GNU that may affect time zone settings, and
using date_default_timezone_set() would also protect from that. This
bug is detailed on the man page for date_default_timezone_set() - to
which a link is provided below.

In most cases, worrying about this is overkill (in my opinion) but the
point of asking for strict error reporting is that you want the
nitpicking. If you don't want this level of nitpicking, you may want
to turn off strict error reporting.

The manual page for date_default_timezone_set() explaing the effect of
strict error reporting on these functions.
PHP Manual - date_default_timezone_set()
http://us2.php.net/manual/en/function.date-default-timezone-set.php

- Hammer


Search strategy
----------------
Searched the PHP Manual for date() and date_default_timezone_set().

Additional Information
----------------------
PHP Manual
http://us2.php.net/manual/en/index.php

Request for Answer Clarification by vladkornea-ga on 19 Jun 2006 12:45 PDT
<i>One is in Eastern time and one is in India, but company
headquarters is in California and wants all transactions stamped with
their local time regardless of origin. Which time zone do you want in
those cases? Good coding practice says "Be explicit and specify which
time zone you intend."</i>

According to the documentation for date_default_timezone_get(), this
is the order in which the time zone is "guessed":

1. The timezone set using the date_default_timezone_set() function (if any)
2. The TZ environment variable (if non empty)
3. The date.timezone ini option (if set)
4. "magical" guess (if the operating system supports it) 

As the warning message I initially listed shows, PHP is forced to use
method 4 to "magically" guess the time zone because none of the first
3 methods are available to it, and this is "unsafe." You seem to
saying that all date_default_timezone_set() does is take the
information it obtained from that guess (4) and calculates a new time
zone from it which then applies for the duration of the script's
execution. For example, it makes a "magical guess" that the server
time zone is 'America/New_York', and then if I do:

date_default_timezone_set('America/Los_Angeles');

it will subtract 3 hours from the system time for the purpose of PHP's
date & time functions.

Is this correct?

Clarification of Answer by hammer-ga on 19 Jun 2006 16:56 PDT
Vladkornea,

I can't be certain of the exact mechanism used by
date_default_timezone_set() without reading the PHP source code. I
expect that it does something along the lines of using the time zone
info reported by the operating system to convert the system time to
GMC, which it would then convert to your requested time zone. This is
close to what you describe, but I expect that, rather than trying to
figure out the differential between the system time zone and yours, it
goes to and from a standard time like GMC.

- Hammer

Clarification of Answer by hammer-ga on 19 Jun 2006 17:17 PDT
GMC should be GMT. Sorry, can't type tonight. Twice.

- Hammer
vladkornea-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy