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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With