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

前端之家收集整理的这篇文章主要介绍了php – 无法解析位置41(i)的时间字符串:双倍时区规范前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 jquery daterangepicker,它反过来使用jQuery datapicker.

我的Ubuntu系统工作正常.浏览器正在发送可解析字符串:

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

输出

  1. DateTime Object
  2. (
  3. [date] => 2012-11-15 00:00:00
  4. [timezone_type] => 1
  5. [timezone] => -07:00
  6. )

在我们的测试者Windows系统中,浏览器正在字符串中发送扩展的时区:

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

抛出异常:

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

我已经google了,找不到关于这个特定的PHP错误的任何资源.

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

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

这似乎很不好,我正在寻找如何正确地做这个建议.

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

我所得到的是:

  1. $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)
  2. print_r($dateStarted);
  3. print_r(\DateTime::getLastErrors());

哪个输出正确的日期:

  1. DateTime Object
  2. (
  3. [date] => 2012-11-15 00:00:00
  4. [timezone_type] => 1
  5. [timezone] => -07:00
  6. )
  7.  
  8. Array
  9. (
  10. [warning_count] => 1
  11. [warnings] => Array
  12. (
  13. [33] => Trailing data
  14. )
  15.  
  16. [error_count] => 0
  17. [errors] => Array
  18. (
  19. )
  20.  
  21. )

格式的最后是使这项工作的魔法.

猜你在找的PHP相关文章