Convert JavaScript new Date() to php DateTime()

19,250

You can get unix timestamp from Date object as follows (see Date.prototype.getTime)

var timestamp = '@' + Math.round(datumUhrzeit.getTime()/1000);

Then when sent on server simply create new datetime object

$datumUhrzeit = new DateTime($timestamp);

If you can't use javascript to create timestamp and you get the the data from form directly you can do something like this, remember to set the timezone:

$datum = $_GET['datum'];
$uhrzeit = $_GET['uhrzeit'];
$datumUhrzeit = DateTime::createFromFormat('Y-m-d H:i:s', $datum . ' ' . $uhrzeit, new DateTimeZone('Europe/Berlin'));

Now as you have saved your date to the database and retrieved it, you can send it back

print $datumUhrzeit->format('U'); // This will print the time as unix timestamp

After that you would create your javascript date object with just the timestamp

var datumUhrzeit = new Date(timestamp * 1000); // timestamp from above

If you for some reason don't want to use unix timestamp you can print it in desired format with format method. Remember to set the timezone beforehand

$datumUhrzeit->setTimezone(new DateTimeZone('Europe/Berlin'));
print $datumUhrzeit->format('Y-m-d H:i:s');

Because javascript doesn't work well with timezones I would advocate you to use unix timestamps when you can. This way you have less problems with timezones.

Share:
19,250
Suisse
Author by

Suisse

Updated on July 26, 2022

Comments

  • Suisse
    Suisse almost 2 years

    I have 2 fields in HTML:

    <input id="datum" type="date">
    <input id="uhrzeit" type="time">
    

    JavaScript:

    var datumUhrzeit = new Date($("#datum").val()+","+$("#uhrzeit").val());
    console.log(datumuhrzeit);
    
     "Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)"
    

    How can I convert "Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)" in PHP to a DateTime, so that I can save it to postgresql?

  • Havenard
    Havenard almost 9 years
    @user3037960 Thats because this code is a mess. Check stackoverflow.com/a/3005959/156811
  • Suisse
    Suisse almost 9 years
    in PHP: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1436525100000) at position 10 (0): Unexpected character
  • Skydda
    Skydda almost 9 years
    Sorry about that :) check it now. You have to divide getTime output with 1000 since it returns milliseconds and unix timestamp uses seconds. Also it requires @ character in front of the timestamp.
  • Suisse
    Suisse almost 9 years
    in javascript: 2015-07-10 12:00:00 in php: 2015-07-10 10:00:00 Should I just add 2hours in php?? what a bricolage!
  • Skydda
    Skydda almost 9 years
    The javascript returns an unix timestamp which is UTC time. When that is parsed in php it shows the time in UTC timezone. You can change the timezone for the DateTime object using php.net/manual/en/datetime.settimezone.php
  • Havenard
    Havenard almost 9 years
    @user3037960 You may think its the same code, but its not. He missed the $ in strtotime(datumUhrzeit) when copying the answer from the other question, in this state the code won't work as expected. I won't edit this answer for this reason, and also because its poorly written.
  • GULIM SHAH
    GULIM SHAH almost 9 years
    at this point it is not important that code from where copied , written the goal is to provide solution to the question......
  • Suisse
    Suisse almost 9 years
    so how can I get the right time back in Javascript? $datumUhrzeit = new DateTime("2015-07-10 11:00:00",new DateTimeZone('UTC')); echo date_format($datumUhrzeit, 'Y-m-d H:i'); So how can I get from 11:00:00 to 13:00:00 (the time whic I had in my html field on the start)
  • Suisse
    Suisse almost 9 years
    no that didn't work, I got "1970-01-01 01:00:00" in the database.
  • rocky
    rocky almost 9 years
    what is your table structure
  • Suisse
    Suisse almost 9 years
    $date = "10 July, 2015"; $time = "13:00"; $dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date . ' ' . $time, new DateTimeZone('Europe/Berlin')); $dateTime is after that just "0" ! What am I doing wrong?
  • Skydda
    Skydda almost 9 years
    The format Y-m-d H:i:s expects the time given to be in format 2015-07-03 14:00:00. If you dont care about seconds drop the :s part from the format string Y-m-d H:i
  • Suisse
    Suisse almost 9 years
    This works: $dateTime = DateTime::createFromFormat('d M, Y H:i', $date . ' ' . $time, new DateTimeZone('Europe/Berlin')); thx
  • Ali Ahmad Pasa
    Ali Ahmad Pasa about 3 years
    I want to do same thing using php
  • Ali Ahmad Pasa
    Ali Ahmad Pasa about 3 years
    No need got it $cheque_date = substr($request['cheque_date'], 0, strpos($request['cheque_date'], '(')); $insertArray['cheque_date'] = date('Y-m-d', strtotime($cheque_date));