Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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

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

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

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.

like image 241
hafichuk Avatar asked Nov 16 '12 17:11

hafichuk


2 Answers

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

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.

like image 178
hafichuk Avatar answered Oct 13 '22 04:10

hafichuk


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

like image 40
Olaf Dietsche Avatar answered Oct 13 '22 03:10

Olaf Dietsche