...">

Convert JavaScript to new date () in php DateTime ()

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)" to PHP to DateTime so that I can store it in postgresql?

+3


source to share


4 answers


You can get unix timestamp from Date object like this ( see Date.prototype.getTime )

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

      

Then when posting to server just create a new datetime object

$datumUhrzeit = new DateTime($timestamp);

      

If you can't use javascript to create the timestamp and you get the data from the form directly, you can do something like this, don't forget 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 that you have saved the date in 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 will create your javascript date object with only timestamp

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

      

If you don't want to use the unix timestamp for some reason, you can print it in the format you want using the format method. Remember to set your time zone in advance

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

      

Since javascript doesn't work with timezones, I would recommend that you use unix timestamps whenever you can. This way you have less time zone problems.

+3


source


You can do something like



$datumUhrzeit = 'Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)';
$datumUhrzeit = substr($datumUhrzeit, 0, strpos($datumUhrzeit, '('));
$resultDate = date('Y-m-d h:i:s', strtotime($datumUhrzeit));
echo $resultDate;

      

0


source


You can use this javascript function to convert dateObject

or date string

to your desired format:

/**
 * Formats a dateObject or date string to Y-m-d date
 * Example: Converts  dateObject or date string  Sat Aug 19 2017 00:00:00 GMT+0530 (India Standard Time)   TO  2017-08-19   
 */
function format_date( date )
{
    if (typeof date == "string")
    {
        date = new Date(date);
    }

    var year = date.getFullYear();
    var month = (1 + date.getMonth()).toString();
    month = month.length > 1 ? month : '0' + month;

    var day = date.getDate().toString();
    day = day.length > 1 ? day : '0' + day;

    return year+'-'+month+'-'+day;
}

var dateString = 'Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)';

var formattedDate = format_date(dateString);//returned formatted date is 2015-08-18

      

Then you can pass this formatted date into your PHP code where you can use a function strtotime

to convert that date to the format you want . For example:

$myFormattedDate = date('d-m-Y', strtotime($_REQUEST['formattedDate']));

      

0


source


try this

 function myFunction() {
      var content =  document.getElementById("datum").value+","+document.getElementById("uhrzeit").value;
  console.log(content);

  }

      

-1


source







All Articles