Skip to content Skip to sidebar Skip to footer

Convert Javascript Time To Mysql Format Using Php

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 ?

Solution 1:

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-1307:00:00

Solution 2:

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;

Solution 3:

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

var time = newDate().getTime() / 1000

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

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

Solution 4:

<?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
    )

)

Post a Comment for "Convert Javascript Time To Mysql Format Using Php"