yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Set default timezone in index.php if not yet defined #209

Closed ShNURoK42 closed 11 years ago

ShNURoK42 commented 11 years ago

I use the Open Server. To install a new application I'm using its version php. Open Server has a "pending" php.ini config option and therefore the following error occurs.

exception 'yii \ base \ ErrorException' with message '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 tho
se methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the t
imezone 'UTC' for now, but please set date.timezone to select your timezone. 'in D: \ OpenServer \ git \ yii2 \ yii \ logging \ Logg
er.php: 97
Stack trace:
#0 [internal function]: yii \ base \ Application-> handleError (2, 'date (): It is n ...', 'D: \ OpenServer \ g ...', 97, Array)
#1 D: \ OpenServer \ git \ yii2 \ yii \ logging \ Logger.php (97): date ('Ymd-His', 1368179900)
#2 D: \ OpenServer \ git \ yii2 \ yii \ base \ Object.php (44): yii \ logging \ Logger-> init ()
#3 D: \ OpenServer \ git \ yii2 \ yii \ YiiBase.php (557): yii \ base \ Object-> __construct ()
#4 D: \ OpenServer \ git \ yii2 \ yii \ YiiBase.php (474): yii \ YiiBase :: getLogger ()
#5 D: \ OpenServer \ git \ yii2 \ yii \ base \ Module.php (455): yii \ YiiBase :: trace ('Loading compone ...', 'yii \ base \ Module ...')
#6 D: \ OpenServer \ git \ yii2 \ yii \ base \ Application.php (277): yii \ base \ Module-> getComponent ('request')
#7 D: \ OpenServer \ git \ yii2 \ yii \ console \ Application.php (92): yii \ base \ Application-> getRequest ()
#8 D: \ OpenServer \ git \ yii2 \ yii \ base \ Application.php (139): yii \ console \ Application-> processRequest ()
#9 D: \ OpenServer \ git \ yii2 \ yii \ yiic.php (22): yii \ base \ Application-> run ()
#10 D: \ OpenServer \ git \ yii2 \ yii \ yiic (13): require_once ('D: \ OpenServer \ g ...')
#11 {main}

I use hook and set date_default_timezone_set('UTC'); in yii\logging\Logger in init() method. Sorry for my bad English :)

samdark commented 11 years ago

Duplicate of #208

bwoester commented 11 years ago

Not entirely sure if it is a duplicate. Seems like the console app causes an error before it even starts executing the app command?

samdark commented 11 years ago

date_default_timezone_set('UTC'); should be done in index.php or php.ini. That's common PHP 5.3+ requirement.

samdark commented 11 years ago

Renamed and reopened.

mdomba commented 11 years ago

As I see this... This is not a requirement for a PHP script but a requirement for a PHP setup... this error means that the option date.timezone in your (developer) PHP.INI is not set.

samdark commented 11 years ago

@mdomba yes. PHP script can correct it. The question is should we correct it or not.

ShNURoK42 commented 11 years ago

Of course correct it. It will be cheaper than to explain all about the ini setting.

bwoester commented 11 years ago

I think I remember a discussion about setting timezone to utc by default anyways...

//EDIT: Here it is: http://www.yiiframework.com/forum/index.php/topic/34055-components-datetime-and-ar/

mdomba commented 11 years ago

If I setup my server (php.ini) on my timezone... than I would certainly not like to have Yii change this by default.

qiangxue commented 11 years ago

How about checking if it is set or not. If not, set it to UTC?

samdark commented 11 years ago

Yes, I think that's the way to go.

Ragazzo commented 11 years ago

I think maybe add in entry point commented line with timezone set? so it will be a notice for user, and will not change defaults as @mdomba pointed to avoid unexpected behavior?

bwoester commented 11 years ago

I think we're doing duplicate work.

There's already Application::getTimeZone() / Application::setTimeZone(). If Application::__construct cared for calling

$this->setTimeZone( ArrayHelper::remove($config,'timeZone','UTC') );

like it does with setBasePath(), everything should be fine. (Notice the use of our brand new, amazingly handy helper! :wink: ).

// EDIT: No wait. This is not what we want...

// EDIT 2: We need to know if the option is set or not.

if (isset($config['timeZone']) || !ini_get('date.timezone')) {
  $this->setTimeZone( ArrayHelper::remove($config,'timeZone','UTC') );
}
ShNURoK42 commented 11 years ago

Yeah, because this code in index.php not help with install a new application.

samdark commented 11 years ago

@bwoester what exactly do you suggest? Putting the code you've provided into index.php?

@ShNURoK42 what do you mean?

ShNURoK42 commented 11 years ago

When use yiic.php to install app, it call yii\logging\Logger init(), where call function date() This leads to exception unless defined time zone. In the first post I wrote about it.

qiangxue commented 11 years ago

I think https://github.com/yiisoft/yii2/commit/810d232ee6717f14f5ecfc4b7b7486ac4d199f26 is fine. It's meant to fix the problem that ideally should be fixed by the user.

Setting timezone via app config is another thing.

samdark commented 11 years ago

@ShNURoK42 fixed.

ShNURoK42 commented 11 years ago

@samdark thanks!

bwoester commented 11 years ago

Oops, I've been a little bit too slow on this, I guess. ^^