我正在使用
jquery
daterangepicker,它反过来使用jQuery
datapicker.
我的Ubuntu系统工作正常.浏览器正在发送可解析字符串:
- $dateStarted = new \DateTime($post['startDate']); // Thu Nov 15 2012 00:00:00 GMT-0700 (MST)
- print_r($dateStarted);
输出:
- DateTime Object
- (
- [date] => 2012-11-15 00:00:00
- [timezone_type] => 1
- [timezone] => -07:00
- )
在我们的测试者Windows系统中,浏览器正在字符串中发送扩展的时区:
- $dateStarted = new \DateTime($post['startDate']); // Thu Nov 15 2012 00:00:00 GMT-0700 (Mountain Standard Time)
- print_r($dateStarted);
抛出异常:
- 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
我已经google了,找不到关于这个特定的PHP错误的任何资源.
我通过分析返回相同结果的括号内的文本来“解决”这个问题:
- $dateString = strstr($dateString," (",true); // Thu Nov 15 2012 00:00:00 GMT-0700
这似乎很不好,我正在寻找如何正确地做这个建议.
使用
DateTime::createFromFormat()作为Marc B建议似乎是一个更好的解决方案.
我所得到的是:
- $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());
哪个输出正确的日期:
- 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
- (
- )
- )
格式的最后是使这项工作的魔法.