How can I convert a js Date (like this Sun Jul 13 2014 07:00:00 GMT+0200 (EET)) to MySQL format (like this 2014-07-13 07:00:00) using php ?
Since your date string already contains the time zone, you don't need to do anything special:
$when = new DateTime('Sun Jul 13 2014 07:00:00 GMT+0200 (EET)');
echo $when->format('Y-m-d H:i:s');
As helpfully noted in comments, this string actually contains two bits of time zone information, UTC + 2 and EET (Eastern European Time), and PHP is basically ignoring the second one. It's spotted better in this example:
var_dump(new DateTime('Sun Jul 13 2014 07:00:00 GMT+0200 (EET)'), DateTime::getLastErrors());
var_dump(new DateTime('Sun Jul 13 2014 07:00:00 GMT+0200'), DateTime::getLastErrors());
var_dump(new DateTime('Sun Jul 13 2014 07:00:00 (EET)'), DateTime::getLastErrors());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2014-07-13 07:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+02:00"
}
array(4) {
["warning_count"]=>
int(1)
["warnings"]=>
array(1) {
[34]=>
string(29) "Double timezone specification"
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2014-07-13 07:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+02:00"
}
array(4) {
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2014-07-13 07:00:00.000000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "EET"
}
array(4) {
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
}
We in fact need to strip one of them, e.g.:
$js_date_string = 'Sun Jul 13 2014 07:00:00 GMT+0200 (EET)';
// Regular expression is shown for illustration purposes, it's probably wrong!
$tmp_date_string = preg_replace('/ GMT\+\d{4}/ui', '', $js_date_string);
$when = new DateTime($tmp_date_string);
var_dump($when, DateTime::getLastErrors());
echo $when->format('Y-m-d H:i:s');
object(DateTime)#1 (3) {
["date"]=>
string(26) "2014-07-13 07:00:00.000000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "EET"
}
array(4) {
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(0)
["errors"]=>
array(0) {
}
}
2014-07-13 07:00:00