3

I have this code:

try{
  $datetime1 = new DateTime("now");
  $datetime2 = new DateTime("now");
  $interval = $datetime1->diff($datetime2);
  var_dump($interval);
}
catch(Exception $e) {
  echo $e->getMessage();
}     

I do not see any result regarding the var_dump, the output of php is:

DateTime::__construct(): It is not safe to rely on the system's timezone setting
s. You are *required* to use the date.timezone setting or the date_default_timez
one_set() function. In case you used any of those methods and you are still gett
ing this warning, you most likely misspelled the timezone identifier. We selecte
d 'Europe/Paris' for '1.0/no DST' instead

I'm using php_cli 5.3.8

What can I do? Thanks

Dail
  • 4,622
  • 16
  • 74
  • 109

2 Answers2

12

In PHP5.3, it's necessary to set the timezone.

Open php.ini and find the setting date.timezone. Uncomment it and set it to some value, such as

date.timezone = "Europe/Paris"

Then restart the server. It should take care of the warning.

[EDIT]

The solution proposed by @greut will work just as well. The difference between the two is that php.ini settings are server-wide (will be applied to all application run now and in the future on the server), while date_default_timezone_set() is applied to the application that's currently executed only.

The php.ini solution lets you forget the warning on any other sites you run - but you need access to php.ini. Using date_default_timezone_set() will override the setting in php.ini, so you can use both approaches at the same time if you wish.

mingos
  • 23,778
  • 12
  • 70
  • 107
  • the error message hints to a solution by programming. plus one for suggesting a solution that doesn't involve code modifications, which will result in more portable code. – davogotland Jan 12 '12 at 12:43
11

Do that in your code:

date_default_timezone_set('Europe/Paris');

or in the php.ini.

But better, read the documentation: http://php.net/date_default_timezone_set

greut
  • 4,305
  • 1
  • 30
  • 49