简体   繁体   中英

php 7.1/7.0 default timezone issue

i am a bit confused while investigating default timezone detection in php 7.1/7.0: WE have a simple php CLI code :

 php -r "var_dump(ini_get('date.timezone'),date_default_timezone_get());"

and according to php.net documentation (http://php.net/manual/en/function.date-default-timezone-get.php ) we should have 'UTC' as a result of date_default_timezone_get function because we do not set timezone in php.ini. And here the output of this code:

Command line code:1:
string(0) ""
Command line code:1:
string(16) "America/Anguilla"

here we can see that ini_get('date.timezone') equals to empty string, but date_default_timezone_get equals to America/Anguilla

i am using: PHP 7.1.12-3+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Dec 14 2017 15:58:40) ( NTS ) .

php -i shows us:

date

date/time support => enabled
timelib version => 2016.05
"Olson" Timezone Database Version => 0.system
Timezone Database => internal
Default timezone => America/Anguilla

By the way timezone on this server is: Asia/Qatar (AST, +0300) , but as i know it should not be taken by php from php 5.4.

Could you help me please to find out where is this value for this timezone was set?

UPDATED: This a php 7.1 source code of timezone detection function:

       static char* guess_timezone(const timelib_tzdb *tzdb)
    {
        /* Checking configure timezone */
        if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
            return DATEG(timezone);
        }
        /* Check config setting for default timezone */
        if (!DATEG(default_timezone)) {
            /* Special case: ext/date wasn't initialized yet */
            zval *ztz;

            if (NULL != (ztz = cfg_get_entry("date.timezone", 

sizeof("date.timezone")))
            && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
            return Z_STRVAL_P(ztz);
        }
    } else if (*DATEG(default_timezone)) {
        if (DATEG(timezone_valid) == 1) {
            return DATEG(default_timezone);
        }

        if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
            php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
            return "UTC";
        }

        DATEG(timezone_valid) = 1;
        return DATEG(default_timezone);
    }
    /* Fallback to UTC */
    return "UTC";
}

In php 5.6 and below i see that php generate a warning in case of timezone not set:

 /* Fallback to UTC */
    php_error_docref(NULL TSRMLS_CC, E_WARNING, DATE_TZ_ERRMSG "We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.");
    return "UTC";

Thanks in advance,

Best Regards,

Dmitry

It's highly probable that the default timezone is set via date_default_timezone_set [https://www.php.net/manual/en/function.date-default-timezone-set.php] either in initialization vendor code or at build-time.

I would argue this is what system packagers should do instead of leaving it undefined. If PHP's argument is that since 5.4, since the value cannot be guessed correctly across all systems, then it shouldn't guess it all, it should leave it up to the packagers and installers to determine the correct value for the system.

I have also heard that the reason the PHP devs did this is because some PHP developers were using the local date-time to present end-user times, which should of course be determined by the user's browser settings or configuration. Date-timezone should only be used for things like logs and geographically bounded installations. But in that case, why remove the default useful guessing, even if it's occasionally wrong?

Either way, PHP determined that it far better to enforce that everyone's PHP installation reports the wrong time by default, rather than this be a curse of the few.

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