简体   繁体   中英

PHP timezone from php.ini not being applied

For some reason the date() function is not outputting the timezone-adjusted date/time. In debugging this, I found a weird disparity. My timezone is defined in php.ini as America/New_York . When I call this:

echo ini_get('date.timezone');

I get America/New_York , which is what I expect. However, when I call this:

echo date_default_timezone_get();

I get UTC . It seems this is responsible for the date() method returning the time in the UTC timezone. Why is this happening? How do I make PHP respect the timezone found in php.ini ?

UPDATE

I am ultimately just trying to get the timezone-adjusted time from date('Ymd G:i:s') . Since my timezone is set in php.ini, how do I get the correct date/time from 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. You can change that by calling this line before the get operation:

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:

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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