php strtotime() 函数返回空白/0

发布于 2024-10-17 08:15:38 字数 677 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-10-24 08:15:38

不太确定为什么要重新组织现有字符串,因为...

echo $timestamp = strtotime('Sat Mar 24 23:59:59 GMT 2012'); 

...工作正常。 (它返回 1332633599,您可以通过 date('r', 1332633599); 检查(这将导致“Sat, 24 Mar 2012 23:59:59 +0000”,所以一切都很好.)

也就是说,如果您要提取字符串的所有组成部分,您不妨使用 mktime<例如:

function texttotime($texttime) {
    if(!$texttime || $texttime=="") return NULL;

    list($junk, $month, $day, $time, $timezone, $year) = explode(' ', $texttime);
    list($hour, $minute, $second) = explode(':', $time);

    return mktime($hour, $minute, $second, $month, $day, $year);
}

Not quite sure why you're re-organising the existing string, as...

echo $timestamp = strtotime('Sat Mar 24 23:59:59 GMT 2012'); 

...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:

function texttotime($texttime) {
    if(!$texttime || $texttime=="") return NULL;

    list($junk, $month, $day, $time, $timezone, $year) = explode(' ', $texttime);
    list($hour, $minute, $second) = explode(':', $time);

    return mktime($hour, $minute, $second, $month, $day, $year);
}
屋檐 2024-10-24 08:15:38

存在 4 小时差异的原因是服务器时区距 GMT 为 -4 小时。
尝试像这样定义您当前的时区:

date_default_timezone_set('Europe/London');

$result = strtotime('Sat Mar 24 23:59:59 GMT 2012');

echo date('r', $result);//Sat, 24 Mar 2012 23:59:59 +0000

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:

date_default_timezone_set('Europe/London');

$result = strtotime('Sat Mar 24 23:59:59 GMT 2012');

echo date('r', $result);//Sat, 24 Mar 2012 23:59:59 +0000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文