简体   繁体   English

将JavaScript new Date()转换为php DateTime()

[英]Convert JavaScript new Date() to php DateTime()

I have 2 fields in HTML: 我有2个HTML字段:

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

JavaScript: 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? 如何将PHP中的“Tue Aug 18 2015 16:45:00 GMT + 0200(MitteleuropäischeSommerzeit)”转换为DateTime,以便将其保存到postgresql?

You can get unix timestamp from Date object as follows ( see Date.prototype.getTime ) 您可以从Date对象获取unix时间戳,如下所示( 请参阅Date.prototype.getTime

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

Then when sent on server simply create new datetime object 然后在服务器上发送时只需创建新的datetime对象

$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: 如果您不能使用javascript创建时间戳并直接从表单获取数据,则可以执行以下操作,请记住设置时区:

$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 之后,您将只使用时间戳创建您的javascript日期对象

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. 如果由于某种原因不想使用unix时间戳,可以使用格式化方法以所需格式打印它。 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. 因为javascript不适用于时区我会主张你尽可能使用unix时间戳。 This way you have less problems with timezones. 通过这种方式,您可以减少时区问题。

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;

You can use this javascript function to convert the dateObject or date string to your desired format: 您可以使用此javascript函数将dateObjectdate string转换为所需的格式:

/**
 * 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 to your PHP code where you can use function strtotime to convert this date to your desired format. 然后,您可以将此格式化日期传递给PHP代码,您可以使用函数strtotime将此日期转换为所需格式。 For ex: 例如:

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

try this one 试试这个

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

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM