简体   繁体   English

来自php.ini的PHP时区未应用

[英]PHP timezone from php.ini not being applied

For some reason the date() function is not outputting the timezone-adjusted date/time. 由于某些原因, date()函数未输出经过时区调整的日期/时间。 In debugging this, I found a weird disparity. 在调试时,我发现了一个奇怪的差异。 My timezone is defined in php.ini as America/New_York . 我的时区在php.ini定义为America/New_York When I call this: 当我这样称呼时:

echo ini_get('date.timezone');

I get America/New_York , which is what I expect. 我得到America/New_York ,这是我所期望的。 However, when I call this: 但是,当我这样称呼时:

echo date_default_timezone_get();

I get UTC . 我得到UTC It seems this is responsible for the date() method returning the time in the UTC timezone. 似乎这是由date()方法负责返回UTC时区中的时间。 Why is this happening? 为什么会这样呢? How do I make PHP respect the timezone found in php.ini ? 如何使PHP尊重php.ini的时区?

UPDATE 更新

I am ultimately just trying to get the timezone-adjusted time from date('Ymd G:i:s') . 我最终只是想从date('Ymd G:i:s')获得时区调整后的时间。 Since my timezone is set in php.ini, how do I get the correct date/time from date() ? 由于我的时区是在php.ini中设置的,如何从date()获取正确的日期/时间?

If I understand the meaning of respect right in your question, you're wondering that date_default_timezone_get() is not returning the ini setting. 如果我了解您的问题中尊重权利的含义,您想知道date_default_timezone_get()没有返回ini设置。 You can change that by calling this line before the get operation: 您可以通过在get操作之前调用以下行来更改它:

date_default_timezone_set(ini_get('date.timezone'));

See as well Example #2 Getting the abbreviation of a timezone from the manual which covers that: 另请参见示例2,从手册中获取时区的缩写,内容涉及:

<?php
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');

The above example will output: 上面的示例将输出:

America/Los_Angeles => America/Los_Angeles => PST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM