- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在研究一些与时间相关的功能,我选择始终使用 UTC 时间并将时间戳存储为整数以保持一致性。
但是,我注意到当我使用mktime
时,当前设置的时区似乎对mktime
的返回值有影响。从文档中我了解到 mktime
应该返回自纪元以来的秒数:
Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
http://php.net/manual/en/function.mktime.php
但是,mktime
似乎包括当前设置的时区。使用以下代码时:
date_default_timezone_set('UTC');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";
date_default_timezone_set('Australia/Sydney');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";
我希望这两个时间值相同,但显然它们不是:
1451606400
1451566800
这似乎正好是 11 小时的时差:
1451606400 - 1451566800 = 39600 / (60*60) = 11
我对 mktime
有什么不正确的理解和/或为什么在使用 mktime
时要考虑时区?
最佳答案
我不能告诉你为什么会这样(PHP 对我来说在日期和时间方面从来没有意义)但是有一个替代函数 gmmktime()
这是
Identical to mktime() except the passed parameters represents a GMT date. gmmktime() internally uses mktime() so only times valid in derived local time can be used.
还有一个comment on the PHP documentation for this function这解释了 mktime()
、gmmktime()
和 time()
是如何工作的。从本质上讲,他们假设您总是在时区中思考,即使 UNIX 时间戳本身不带有时区也是如此。
关于PHP mktime 和时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36095947/
我想从 string 得到一个 unix 时间戳包含形式 YYYYMMDDThhmmss+TZ 的时间表示. 为此,我将字符串转换为 struct tm然后使用 mktime将其转换为unix时间戳。
这是一个 PHP 问题。当我测试这段代码时 echo date("d-m-y h:i:S"); 在我的本地服务器和我的托管网站(这两个具有不同的时区)上,它们返回不同的日期时间值,正如预期的那样。但是
我想在微 Controller 上使用 mktime(或者至少使用 32 位宽的时间戳)。我从 avr libc source files 添加了所需的文件(在 Atmel Studio 7 中,时间
我正在将 azure-sdk 用于 python,特别是 azure-servicebus 0.50.0 from azure.servicebus import QueueClient servic
我在 mktime() 中遇到了年份问题。 每次我将低于 1970 年的年份传递到 struct tm 中,然后运行 mktime() 函数来转换我的结构时,都会失败(返回 -1) 。 有人知道为
我正在开发按含义分隔字符的程序。目前我遇到了 konverzia 函数的问题。它应该在日期字符串中添加日期名称(星期三、星期四、星期五...),但它根本不在乎。日期必须类似于周三 2012-02-01
我正在尝试将以 HHMMSS.SS,DD,MM,YYYY 格式给出的时间转换为 unix 时间。问题是,然后调用 mktime,返回相同的 time_t。 ConvertTime(std::strin
我有一个日期操作类。当我尝试用相同的值初始化它时,第一个实例工作正常,但所有其他实例每次都返回不同的值。我将日期保存在 time_t DATE 变量中。 它的构造函数: CDate (int y,in
在一个简单的测试中,获取从 de Unix Epoch (01/01/1970 00:00:00 UTC) 到 2019 年 2 月 22 日的耗时(以秒为单位),它返回了大量的 1844674407
我正在尝试将一周重复添加到包含日期信息的事件结构中。我这样做是为了在某个时间之前创建一个事件的多个实例。我的 mktime 函数出现段错误,其中 full_time = mktime(&caltime
我想用澳大利亚/悉尼时区构造一个struct tm,所以我首先使用: setenv("TZ","Australia/Sydney",1); tzset() 然后我将struct tm设置为: stru
这是将 Fri Jan 1 00:00:00 IST 1970 转换为 EPOCH 的代码段 memset(&Date_st,0,sizeof(struct tm)); Date_st.tm_ye
我正在尝试将用户输入日期添加到现有时间结构中,但我收到了一个我不明白的错误。当我试图修改月份日期时,编译器告诉我工作日超出范围。代码贴在下面。 struct tm date; int m, d, y,
这个问题在这里已经有了答案: C++ mktime returning random dates (3 个答案) 关闭 6 年前。 我准备了以下结构: struct tm tDepartureTim
在尝试编写返回比给定时间少 24 小时的代码时,mktime() 显示不一致的输出。我的计算类似于:current_time(GMT) - 86400 应该返回正确的值。我们需要做的就是根据输入的时间
这个问题已经有答案了: Python | mktime overflow error (2 个回答) 已关闭 6 年前。 使用 Python 时 time模块我收到此错误: OverflowError
继续 my attempt to create a DateTime class ,我试图在我的函数中存储“纪元”时间: void DateTime::processComponents(int mo
我现在正在使用 gcc 编译器在我的 RedHat Linux 机器上编写一个 C 程序,它将接受两个日期作为输入并计算它们之间的差异。 但是,我发现 mktime() 有一个奇怪的行为,如下面的代码
我做的一个网站突然出现以下错误,到目前为止一直运行良好: A PHP Error was encountered Severity: Warning Message: mktime() [functi
我正在研究一些与时间相关的功能,我选择始终使用 UTC 时间并将时间戳存储为整数以保持一致性。 但是,我注意到当我使用mktime 时,当前设置的时区似乎对mktime 的返回值有影响。从文档中我了解
我是一名优秀的程序员,十分优秀!