Wrong PHP date() output in wamp server

2019-06-15 20:03发布

The problem is that date('r') returns wrong time for my timezone.

php.ini timezone setting:
date.timezone = Europe/Kiev

date_default_timezone_set('Europe/Kiev') in my script solves the problem.

So what's wrong with WAMP?

7条回答
萌系小妹纸
2楼-- · 2019-06-15 20:26

According to the documentation of date_default_timezone_get, the date.timezone configuration option can be overridden by setting the TZ environement variable (which, in turn, can be overridden by calling date_default_timezone_set). From your description, I suspect that the TZ environement veriable is set.

查看更多
Bombasti
3楼-- · 2019-06-15 20:32

I suggest always using date_default_timezone_set() from script

e.g.

date_default_timezone_set('Europe/Kiev');

or

ini_set('date.timezone', 'Europe/Kiev');

...to avoid PHP guessing timezone.

It comes handy when you transfer code to different server(s), for example, outside of Ukraine. This line should help you to avoid unexpected (wrong) results if date.timezone is not set in php.ini or its setting is incorrect. It's also handy when you can't access and/or modify php.ini (shared hosting).

Also, be sure that you've not used ; at the and of line in php.ini.

Restart server after changing php.ini.

查看更多
Rolldiameter
4楼-- · 2019-06-15 20:38

I know this is an old question. If you are using a PHP framework, you might want to check the config file of the framework. For example in Laravel, open the config/app.php and you will find timezone there. Set it to your timezone.

查看更多
狗以群分
5楼-- · 2019-06-15 20:42

You need to reload the configuration / restart the server after editing your php.ini file.

查看更多
冷血范
6楼-- · 2019-06-15 20:42

by default it shows GMT time you can change for your region with following code

   date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
    echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time

List of Supported Timezones http://www.php.net/manual/en/timezones.php

查看更多
仙女界的扛把子
7楼-- · 2019-06-15 20:48

Edit php.ini and restart Apache:

  • left click to WampServer in tray icon
  • open php.ini (go to PHP -> php.ini)
  • set new date.timezone value
    ;date.timezone = UTC 
    date.timezone = Europe/Kiev
  • restart Apache ( go to Apache -> Service -> Restart Service )
  • check value of date.timezone by phpinfo();
查看更多
登录 后发表回答