php strtotime() 函数返回空白/0
我有这样的字符串格式的日期时间数据:
Sat Mar 24 23:59:59 GMT 2012
我想将其转换为 UTC 时间戳,但是当我按如下方式尝试时:
function texttotime($texttime)
{
if(!$texttime || $texttime=="")
return NULL;
// Sat Mar 24 23:59:59 GMT 2012
$bits = preg_split('/\s/', $texttime);
// Mar 24 2012 23:59:59 GMT
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3] bits[4]");
}
它输出 0 (不是 NULL)。
如果我将最后一行更改为:
// Mar 24 2012 23:59:59
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3]");
它会输出一些内容(但时间戳错误,相差 -4 小时左右)。
I have datetime data in string format like this:
Sat Mar 24 23:59:59 GMT 2012
I want to convert this into a UTC timestamp, but when I try it as follows:
function texttotime($texttime)
{
if(!$texttime || $texttime=="")
return NULL;
// Sat Mar 24 23:59:59 GMT 2012
$bits = preg_split('/\s/', $texttime);
// Mar 24 2012 23:59:59 GMT
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3] bits[4]");
}
It outputs 0 (not NULL).
If I change the last line to:
// Mar 24 2012 23:59:59
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3]");
It outputs something (but the wrong timestamp, off by -4 hours or so).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太确定为什么要重新组织现有字符串,因为...
...工作正常。 (它返回 1332633599,您可以通过
date('r', 1332633599);
检查(这将导致“Sat, 24 Mar 2012 23:59:59 +0000”,所以一切都很好.)也就是说,如果您要提取字符串的所有组成部分,您不妨使用 mktime<例如:
Not quite sure why you're re-organising the existing string, as...
...works correctly. (It returns 1332633599, which you can check via
date('r', 1332633599);
(This will result in "Sat, 24 Mar 2012 23:59:59 +0000", so all is well.)That said, if you're going to extract all of the components of the string, you might as well use mktime. For example:
存在 4 小时差异的原因是服务器时区距 GMT 为 -4 小时。
尝试像这样定义您当前的时区:
The reason you have 4 hour difference is caused by server timezone which is -4 hours from GMT.
try to define your current timezone like this: