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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31210842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:27:45  来源:igfitidea点击:

Convert JavaScript new Date() to php DateTime()

javascriptphpdatedatetime

提问by Suisse

I have 2 fields in HTML:

我在 HTML 中有 2 个字段:

<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?ische Sommerzeit)”转换为 DateTime,以便将其保存到 postgresql?

采纳答案by Skydda

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

然后在服务器上发送时只需创建新的日期时间对象

$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. Remember to set the timezone beforehand

如果您出于某种原因不想使用 unix 时间戳,您可以使用 format 方法以所需的格式打印它。记得事先设置好时区

$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.

因为 javascript 不适用于时区,所以我建议您尽可能使用 unix 时间戳。这样你的时区问题就少了。

回答by Rahul Gupta

You can use this javascript function to convert the dateObjector date stringto your desired format:

您可以使用此 javascript 函数将dateObject或转换date 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 strtotimeto convert this date to your desired format. For ex:

然后您可以将此格式化的日期传递给您的 PHP 代码,您可以在其中使用函数strtotime将此日期转换为您想要的格式。例如:

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

回答by rocky

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;

回答by GULIM SHAH

try this one

试试这个

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

  }