2

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 ?

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

This does not work for Berlin I guess. Thats my js date: Mon May 04 2020 00:00:00 GMT+0200 (Central European Summer Time). When trying to create a new DateTime I receive the error Double timezone specification.
@Adam It's probably what the error message says: you have two time zones. That they're identical half of the year doesn't change that. What if you add several months? do you want to result to be UTC+2 or CEST?
@ÁlvaroGonzález at the moment they are the same, and I guess the other half of the year js will print GMT+0100 (Central European Summer Time). In your example the string has also two timezones GMT+0200 and EET, but that works for some reason.
@Adam Ah, sorry, I hadn't noticed that. Interesting... In the question's example PHP is in fact ignoring the EET part. Perhaps we just hit a loophole in the parser's code.
haha, yes maybe. But one can fix it by removing the second timestamp with regex like this: $cleanDate = preg_replace('/\(.*\)/', '', $jsDate);.
|
1

I will try to enhance @akuzminsky answer a little. Since date_parse is using strtotime internally to parse the date, you can use it directly and make your code more flexible.

There is a small drawback since you need to set the time zone in order to use date correctly and avoid php warning. I am setting it to Europe/Athens but you can find here all available codes.

date_default_timezone_set("Europe/Athens");

$unixTimeStamp = strtotime("Sun Jul 13 2014 07:00:00 GMT+0200 (EET)");
$newFormat = date('Y-m-d H:i:s', $unixTimeStamp );

echo $newFormat;

Comments

1

Javascript: This get this time right now in seconds. Dividing by 1000 gets rid of the milliseconds

var time = new Date().getTime() / 1000

PHP: Use the time returned from JS, the integer value

$time = gmdate('H:i:s', time)

Comments

0
<?php
print_r(date_parse("Sun Jul 13 2014 07:00:00 GMT+0200"));
?>


Array
(
[year] => 2014
[month] => 7
[day] => 13
[hour] => 7
[minute] => 0
[second] => 0
[fraction] => 0
[warning_count] => 0
[warnings] => Array
    (
    )

[error_count] => 0
[errors] => Array
    (
    )

[is_localtime] => 1
[zone_type] => 1
[zone] => -120
[is_dst] =>
[relative] => Array
    (
        [year] => 0
        [month] => 0
        [day] => 0
        [hour] => 0
        [minute] => 0
        [second] => 0
        [weekday] => 0
    )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.