How to fix warning from date() in PHP"

133,987

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
133,987
Nano HE
Author by

Nano HE

A newbie, China based; And I am new to programming. Work mostly with C#, C++, Perl & Php; Using OS: winxp; C#/C++: Visual Studio 2005/2008; Perl: Active Perl 5.10.1 + Komodo Edit 6; Php: NetBeans IDE 6.7 + XAMPP 1.7.3 Thanks for reading and for the replies! ;) I'm Reading Magic Tree House

Updated on March 13, 2020

Comments

  • Nano HE
    Nano HE about 4 years

    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
    Nano HE about 13 years
    I found it. Done and works now. date.timezone = Asia/Shanghai
  • Mirko
    Mirko about 11 years
    This is the only method that works
  • Frank
    Frank about 11 years
    This answer is not working in my case. Check-Here: stackoverflow.com/questions/15807087/… How do i use ini_set().
  • Admin
    Admin over 10 years
    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?
  • Poelinca Dorin
    Poelinca Dorin over 10 years
    If you travel around the globe, your OS timezone does change ? If so you can check this out and set it properly stackoverflow.com/questions/4700156/… 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 ?
  • Admin
    Admin over 10 years
    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...
  • Poelinca Dorin
    Poelinca Dorin over 10 years
    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 :)
  • Alex Pliutau
    Alex Pliutau over 10 years
    @Frank, you can date_default_timezone_set('Europe/Minsk');
  • ChrisR
    ChrisR about 10 years
    Just a heads up for anybody wondering but ini_alter is just a lesser known alias of ini_set
  • Hamman Samuel
    Hamman Samuel over 7 years
    List of valid values for date.timezone php.net/manual/en/timezones.php