35

I am using XAMPP(PHP Version 5.3.1) on winxp. When I try to call time() or date() function on my localhost. It will show warning message as this,

Severity: Warning

Message: date() [function.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 'UTC' for '8.0/no DST' instead

Filename: helpers/date_helper.php

How can I disable the warning? Thanks.

Nano HE
  • 9,109
  • 31
  • 97
  • 137

5 Answers5

60

Try to set date.timezone in php.ini file. Or you can manually set it using ini_set() or date_default_timezone_set().

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
19

You need to set the default timezone smth like this :

date_default_timezone_set('Europe/Bucharest');

More info about this in http://php.net/manual/en/function.date-default-timezone-set.php

Or you could use @ in front of date to suppress the warning however as the warning states it's not safe to rely on the servers default timezone

Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • 1
    Thanks for several options and a link to the manual. However nor the manual nor the warning are very clear. What is not safe about it. I want to use the local time. And when I travel with my computer and I change my timezone I want it to use that instead of some stale version in php.ini. There is a reason we have a system time no? –  Sep 20 '13 at 17:17
  • If you travel around the globe, your OS timezone does change ? If so you can check this out and set it properly http://stackoverflow.com/questions/4700156/php-how-to-get-the-server-time-zone if not, then you could do a geoip lookup but you'll run into different problems (each request a user makes, your server makes another one, so you need to store the value and so on). A server is not meant to travel around the globe is it ? – Poelinca Dorin Sep 20 '13 at 17:32
  • 1
    sure, servers are usually stationary, even though they don't need to be necessarily. However I'm not only using php for server applications, so yeah, when I travel I set my OS timezone to see the correct time. And even though there are lots of pages on the internet about this warning, no one explains what's not safe about it... –  Sep 20 '13 at 18:40
  • Most of php applications are websites, and the majority of them are hosted on shared servers ... so the system time is not what most of developers expect for their targeted audience. p.s. this is just my common sense :) – Poelinca Dorin Sep 20 '13 at 19:16
10

You could also use this:

ini_alter('date.timezone','Asia/Calcutta');

You should call this before calling any date function. It accepts the key as the first parameter to alter PHP settings during runtime and the second parameter is the value.

I had done these things before I figured out this:

  1. Changed the PHP.timezone to "Asia/Calcutta" - but did not work
  2. Changed the lat and long parameters in the ini - did not work
  3. Used date_default_timezone_set("Asia/Calcutta"); - did not work
  4. Used ini_alter() - IT WORKED
  5. Commented date_default_timezone_set("Asia/Calcutta"); - IT WORKED
  6. Reverted the changes made to the PHP.ini - IT WORKED

For me the init_alter() method got it all working.

I am running Apache 2 (pre-installed), PHP 5.3 on OSX mountain lion

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sumeet Basak
  • 180
  • 1
  • 8
3

This just happen to me because in the php.ini the date.timezone was not set!

;date.timezone=Europe/Berlin

Using the php date() function triggered that warning.

cottton
  • 1,522
  • 14
  • 29
0
error_reporting(E_ALL ^ E_WARNING);

:)

You should change subject to "How to fix warning from date() in PHP"...

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66