Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date.timezone not working correctly for command line script only

Tags:

php

I have this entry in my php.ini file:

date.timezone = 'Europe/London';

But every time i use the DateTime() in a command line script i still get the following error:

Exception: DateTime::__construct(): 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 'Europe/London' for 'BST/1.0/DST' instead

I therefore have to do the following in any scripts to get this to work:

date_default_timezone_set('Europe/London');

What else can be causing this error?

UPDATE

I used the following in both command line and web browser:

<?php

var_dump(ini_get('date.timezone'));
exit;

In command line i get:

string(0) ""

In web browser i get:

string(13) "Europe/London" 
like image 250
user1572427 Avatar asked Aug 24 '12 11:08

user1572427


People also ask

What is PHP default timezone?

Reading the timezone set using the date_default_timezone_set() function (if any) Reading the value of the date. timezone ini option (if set) If none of the above succeed, the default timezone will be UTC.

How can I get timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.


2 Answers

CLI and webserver (and cgi and fpm) use different php.ini-files. Because you say you see the right value in you browser I guess you edited the wrong one. On cli type

php --ini

To find out, which is the one you must edit. It's the one in the second line, for example

Loaded Configuration File:         /etc/php5/cli/php.ini
like image 71
KingCrunch Avatar answered Oct 12 '22 13:10

KingCrunch


Some servers(Like media temple) do the configurations for the particular domains.So the server will access the common php.ini file which is under the /etc/php.ini but the browser access the domain's php.ini file.It may occurs the problem

like image 25
Kalai Avatar answered Oct 12 '22 11:10

Kalai