将 PHP 日期转换为 javascript 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10837022/
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-08-24 03:08:40  来源:igfitidea点击:

Convert PHP date into javascript date format

javascriptphpjsondatetimedate-conversion

提问by LeeTee

I have a PHP script that outputs an array of data. This is then transformed into JSONusing the json_encode()function.

我有一个输出数据数组的 PHP 脚本。然后将其转换为JSON使用该json_encode()函数。

My issue is I have a date within my array and it's not in the correct JavaScript format. How can I convert this within PHP so it is?

我的问题是我的数组中有一个日期,但它的 JavaScript 格式不正确。我怎样才能在 PHP 中转换它呢?

$newticket['ThreadID'] =  $addticket;
$newticket['Subject'] =  $subject;
//$newticket['DateCreated'] =  date('d-m-Y G:H');

Instead of the above for the date I need the equivalent of the JavaScript function

而不是上面的日期,我需要等效的 JavaScript 函数

new Date()

新日期()

When I output the above I get the following "Fri Jun 01 2012 11:08:48 GMT+0100 (GMT Daylight Time)" However, If I format my PHP date to be the same, then JavaScript rejects it. Confused...

当我输出上述内容时,我得到以下信息“Fri Jun 01 2012 11:08:48 GMT+0100(GMT Daylight Time)”但是,如果我将 PHP 日期格式设置为相同,则 JavaScript 会拒绝它。使困惑...

Can anyone help?

任何人都可以帮忙吗?

回答by jeremyharris

You should probably just use a timestamp

您可能应该只使用时间戳

$newticket['DateCreated'] = strtotime('now');

Then convert it to a Javascript date

然后将其转换为 Javascript 日期

// make sure to convert from unix timestamp
var now = new Date(dateFromPHP * 1000);

回答by Javlonbek

Javascript Date class supports ISO 8601 date format so I would recommend:

Javascript Date 类支持 ISO 8601 日期格式,所以我建议:

<?php 
      date('c', $yourDateTime); 
      // or for objects
      $dateTimeObject->format('c');
?>

documentation says that: format character 'c' is ISO 8601 date (added in PHP 5)
example: 2004-02-12T15:19:21+00:00

文档说:格式字符 'c' 是 ISO 8601 日期(在 PHP 5 中添加)
示例:2004-02-12T15:19:21+00:00

for more information: http://php.net/manual/en/function.date.php

更多信息:http: //php.net/manual/en/function.date.php

回答by Rahul Gupta

It is pretty simple.

这很简单。

PHP code:

PHP代码:

$formatted_date = $newticket['DateCreated'] =  date('Y/m/d H:i:s');

Javascript code:

Javascript代码:

var javascript_date = new Date("<?php echo $formatted_date; ?>");

回答by Tiago Buzatto

Is very simple, I'm use this:

很简单,我用这个:

new Date("<?= date('Y/m/d H:i:s'); ?>");

回答by xarlymg89

An improvement or simplification of @jeremyharris answer would be this one:

@jeremyharris 答案的改进或简化是这样的:

DateTime objects in PHP have the getTimestamp()format, use it and multiply the value by 1000:

PHP 中的 DateTime 对象具有getTimestamp()格式,使用它并将值乘以 1000:

<?php 
    $phpDateTimeStamp = new Date('Y/m/d H:i:s')->getTimestamp() * 1000; 
?>

// JavaScript
let startTime = new Date(phpDateTimeStamp);

回答by Scott Saunders

$newticket['DateCreated'] = date('d-m-Y G:H', strtotime($phpDateVariable));

回答by Joan-Diego Rodriguez

If you want to be more precise with your timestamp, you should use microtime() instead of now().

如果您想更精确地使用时间戳,您应该使用 microtime() 而不是 now()。

That gives you:

这给你:

echo round(microtime(TRUE)*1000);

For a milisecond, javascript-like timestamp in php.

一毫秒,php 中类似 javascript 的时间戳。