php 无法解析位置 41 (i) 处的时间字符串:双时区规范

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13421635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:22:23  来源:igfitidea点击:

Failed to parse time string at position 41 (i): Double timezone specification

phpjquery

提问by hafichuk

I'm using the jquery daterangepicker, which in turn uses the jQuery datapicker.

我正在使用 jquery daterangepicker,而后者又使用 jQuery datapicker

My Ubuntu system works fine. The browser is sending a parseable string:

我的 Ubuntu 系统运行良好。浏览器正在发送一个可解析的字符串:

$dateStarted = new \DateTime($post['startDate']); // Thu Nov 15 2012 00:00:00 GMT-0700 (MST)
print_r($dateStarted);

Outputs:

输出:

DateTime Object
(
    [date] => 2012-11-15 00:00:00
    [timezone_type] => 1
    [timezone] => -07:00
)

On our testers Windows system, the browser is sending an expanded timezone in the string:

在我们的测试人员 Windows 系统上,浏览器在字符串中发送一个扩展的时区:

$dateStarted = new \DateTime($post['startDate']); // Thu Nov 15 2012 00:00:00 GMT-0700 (Mountain Standard Time)
print_r($dateStarted);

Throws and exception:

抛出和异常:

Exception: DateTime::__construct(): Failed to parse time string 
 (Thu Nov 15 2012 00:00:00 GMT-0700 (Mountain Standard Time)) 
 at position 41 (i): Double timezone specification

I've googled around and can't find any resources on this specific PHP error.

我已经用谷歌搜索了,但找不到关于这个特定 PHP 错误的任何资源。

I'm "solving" this problem by striping out the bracketed text which returns the same results:

我通过去除返回相同结果的括号文本来“解决”这个问题:

$dateString = strstr($dateString, " (", true); // Thu Nov 15 2012 00:00:00 GMT-0700

This seems pretty bad to do and I'm looking for suggestions on how to do this properly.

这似乎很糟糕,我正在寻找有关如何正确执行此操作的建议。

回答by hafichuk

Using DateTime::createFromFormat()as Marc B suggested seems to be a better solution.

使用Marc B 建议的DateTime::createFromFormat()似乎是一个更好的解决方案。

What I've ended up with is:

我最终得到的是:

$dateStarted = \DateTime::createFromFormat('D M d Y H:i:s e+', $post['startDate']); // Thu Nov 15 2012 00:00:00 GMT-0700 (Mountain Standard Time)
print_r($dateStarted);
print_r(\DateTime::getLastErrors());

Which outputs the correct date now:

现在输出正确的日期:

DateTime Object
(
    [date] => 2012-11-15 00:00:00
    [timezone_type] => 1
    [timezone] => -07:00
)

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [33] => Trailing data
        )

    [error_count] => 0
    [errors] => Array
        (
        )

)

The +at the end of the format is the magic that makes this work.

+在格式到底是什么使得这个工作的魔力。

回答by Olaf Dietsche

I would say this is a bug. You get the same error, when using this string

我会说这是一个错误。使用此字符串时,您会遇到相同的错误

$dateStarted = new \DateTime("Thu Nov 15 2012 00:00:00 GMT-0700 (abcdefg)");

One less

少一个

$dateStarted = new \DateTime("Thu Nov 15 2012 00:00:00 GMT-0700 (abcdef)");

and it is parsed "properly".

它被“正确”解析。

It seems the time zone string is restricted to 6 characters. Unless you can and are willing to configure your Windows clients, I would say stripping the "time zone" is a viable "solution".

似乎时区字符串限制为 6 个字符。除非您可以并且愿意配置您的 Windows 客户端,否则我会说剥离“时区”是一个可行的“解决方案”。

回答by Gayan Kavirathne

I'm working in php 7.2 with laravel 5.6. Most upvoted unswer is not working since that method is not found in this php version. (I'm not sure, but it gives an error saying that method not found). But instead there is a method which behaves exactly the same as that which solves the original issue (Converting a javascript date object to timestamp).

我在 php 7.2 和 laravel 5.6 中工作。大多数赞成的 unswer 不起作用,因为在这个 php 版本中找不到该方法。(我不确定,但它给出了一个错误,说找不到该方法)。但是,有一种方法的行为与解决原始问题的方法完全相同(将 javascript 日期对象转换为时间戳)。

$fromDate = date_create_from_format('D M d Y H:i:s e+', $fromDate);

This can then be used in db queries or as a date object.

然后可以将其用于数据库查询或用作日期对象。