php 如何设置时区 xampp mysql 和 apache?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26269364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setup timezone xampp mysql and apache?
提问by AirForce
I am using XAMPP - PHP and MYSQL servers. When I tried to use following -
我正在使用 XAMPP - PHP 和 MYSQL 服务器。当我尝试使用以下时 -
getRates(date('Y-m-d'));
function getRates($cDate)
{
$query = "SELECT * FROM randa WHERE date like '$cDate'" //it only worked at times.
}
?>
Then I realized the date('Y-m-d') does not return the correct date. Went to php.ini and changed time zone. And is still returning the wrong date.
然后我意识到 date('Ym-d') 没有返回正确的日期。转到 php.ini 并更改时区。并且仍然返回错误的日期。
How can I fix this ? Thank you
我怎样才能解决这个问题 ?谢谢
回答by Rulisp
Try this
尝试这个
1) In httpd.conf (\xampp\apache\conf\httpd.conf) , add the following line:
1) 在 httpd.conf (\xampp\apache\conf\httpd.conf) 中,添加以下行:
SetEnv TZ Europe/Moscow
2) Edit php.ini (\xampp\php\php.ini) date.timezone value in [Date] section:
2) 在 [Date] 部分编辑 php.ini (\xampp\php\php.ini) date.timezone 值:
date.timezone = "Europe/Moscow"
3) In my.ini (\xampp\mysql\bin\my.ini) add or replace
3)在my.ini(\xampp\mysql\bin\my.ini)中添加或替换
default-time-zone = "Europe/Moscow"
Restart Apache and MySQL
重启 Apache 和 MySQL
回答by Putra L Zendrato
Please add this code on your top page.
请将此代码添加到您的首页。
date_default_timezone_set('America/Los_Angeles');
Search your country here http://php.net/manual/en/timezones.php
在此处搜索您所在的国家/地区 http://php.net/manual/en/timezones.php
回答by AirForce
Solved: but feeling stupid.
已解决:但感觉很愚蠢。
my local machine had the correct time however, time zone was incorrect. Changed the time zone and it worked. I don't understand why though. The time WAS CORRECT. Zone was not.
我的本地机器有正确的时间,但是时区不正确。更改了时区并且它起作用了。我不明白为什么。时间是正确的。区不是。
回答by Richard McGee
MySQL stores datetime as [yyyy-mm-dd hh:mm:ss] at the UTC timezone. Your field in the db may be set up incorrectly but it may help if you post it's parameters. You shouldn't need to change settings in the conf or php.ini files. I suspect the difference between UTC and your timezone is making the query fail at certain times of the day because the UTC timestamp has passed into the next day. You may need to compensate your date prior to making the query by adding (or subtracting) the right amount of time so the datetime matches UTC. Something like this: getRates(date("Y-m-d h:i:sa",(yourtimestamp + (7 * 60 * 60)))); Or declare the timezone prior to the query: date_default_timezone_set("America/New_York");
MySQL 在 UTC 时区将日期时间存储为 [yyyy-mm-dd hh:mm:ss]。您在数据库中的字段可能设置不正确,但如果您发布它的参数可能会有所帮助。您不需要更改 conf 或 php.ini 文件中的设置。我怀疑 UTC 和您的时区之间的差异使查询在一天中的某些时间失败,因为 UTC 时间戳已传递到第二天。在进行查询之前,您可能需要通过添加(或减去)正确的时间量来补偿您的日期,以便日期时间与 UTC 匹配。像这样: getRates(date("Ymd h:i:sa",(yourtimestamp + (7 * 60 * 60)))); 或者在查询之前声明时区: date_default_timezone_set("America/New_York");