【发布时间】:2011-12-25 01:49:26
【问题描述】:
当前的 Unix 纪元是 PHP UTC 吗?还是当地时间?
【问题讨论】:
标签: php unix-timestamp
当前的 Unix 纪元是 PHP UTC 吗?还是当地时间?
【问题讨论】:
标签: php unix-timestamp
一般来说,无论编程语言如何,Unix 纪元都是 UTC。
来自http://en.wikipedia.org/wiki/Unix_time:
Unix 纪元是 1970 年 1 月 1 日 00:00:00 UTC 时间(或 1970-01-01T00:00:00Z ISO 8601)。
【讨论】:
正如 Trott 所说,UNIX 纪元是 1970 年 1 月 1 日 UTC,但 PHP 纪元在您当前的时区。我使用此脚本将其打印出来。当我使用这个脚本打印出来时,它总是在我当前的时区。
<?php
date_default_timezone_set("US/Pacific");
print_r(array(
'$argv' => $argv,
'strtotime' => strtotime($argv[1]),
'date' => date("H:i:s m/d/Y", strtotime($argv[1])),
));
这里有一个指向 PHP 源代码 strtotime() 的链接,您可以在函数中读取它获取时区的位置。
PHP_FUNCTION(strtotime)
{
zend_string *times;
int error1, error2;
timelib_error_container *error;
zend_long preset_ts = 0, ts;
timelib_time *t, *now;
timelib_tzinfo *tzi;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(times)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(preset_ts)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
tzi = get_timezone_info();
now = timelib_time_ctor();
now->tz_info = tzi;
now->zone_type = TIMELIB_ZONETYPE_ID;
timelib_unixtime2local(now,
(ZEND_NUM_ARGS() == 2) ? (timelib_sll) preset_ts : (timelib_sll) time(NULL));
t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
error1 = error->error_count;
timelib_error_container_dtor(error);
timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
timelib_update_ts(t, tzi);
ts = timelib_date_to_int(t, &error2);
timelib_time_dtor(now);
timelib_time_dtor(t);
if (error1 || error2) {
RETURN_FALSE;
} else {
RETURN_LONG(ts);
}
}
【讨论】: