How to convert a GMT timestamp to proper Date in PHP?

PHP date() relying on the system's timezone settings

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

    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

vladkornea-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.