Send PHP date to JavaScript date format

感情迁移 提交于 2019-11-26 12:26:51

问题


I want to pass my PHP server time to my JavaScript file.

PHP Code:

date_default_timezone_set(\'Australia/Perth\');
echo date(\"r\");

JavaScript:

$.get(\'time.php\', function(data) {
  today = new Date(data);
  closing = new Date(data);
});

The PHP code returns Sun, 18 Mar 2012 12:01:23 +0800 which is correct time for Australia/Perth. But this returns an invalid JavaScript date object.

When I try to convert it to timestamp like:

 echo strtotime(date(\"r\"));

I get the JavaScript date Sun Mar 18 2012 04:03:14 GMT+0000 (WET) (this is the value of today js var)

If I use:

echo gmstrftime(\'%s\');

I get: Sat Mar 17 2012 20:04:30 GMT+0000 (WET).

Can anyone please help me out?


回答1:


The PHP code in Luna's answer with echo date isn't exactly like JavaScript code. This will mimic the JavaScript code exactly:

echo date('D M d Y H:i:s O');



回答2:


$.get('time.php', function(data) {
  today = new Date(data);
  closing = new Date(data);
});

What was the purpose of multiplying the string by 1000? That operation doesn't make sense.

This PHP will work for that.

echo date('D, d M y H:i:s')." +0000";



回答3:


date('D M d Y H:i:s O')

It won't work if your current locale isn't English.

A better alternative is to use:

new Date(<? echo date("Y, n - 1, d, H, i, s") ?>)



回答4:


You could also just leave the PHP code as it is and parse the date using JavaScript:

var date = new Date(Date.parse(DATE));

Then even things like this would work:

new Date(Date.parse('11 March 2017'));

Which outputs via a console log (GMT+1000 is because I am in Australia):

Sat Mar 11 2017 00:00:00 GMT+1000

More information is here: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Date/parse




回答5:


Here is an example with the DateTime object:

PHP code (works on PHP 5.3 or later)

$serverDate = new \DateTime('NOW');

// If you want to set a different time zone
// $serverDate = new \DateTime('NOW', new \DateTimeZone('Australia/Perth'));
echo $serverDate->format(DATE_ATOM);

JavaScript code

$.get('time.php', function(data) {
  var serverDate = new Date(data);
});



回答6:


A good way is timestamp:

echo $data = time()*1000;
echo '
  <div id="setxDatetime">The current server time is: </div>
  <script type="text/javascript">
    var x = document.getElementById("setxDatetime");
    x.innerHTML = x.innerHTML + new Date(' . $data . ');
  </script>
';

1381324693000

The current server time is: Wed Oct 09 2013 16:18:13 GMT+0300 (GTB Standard Time)




回答7:


There might be better solutions, but this one did the trick for me. The key issue is that JavaScript uses months 0-11, while PHP uses 1-12 as mentioned previously.

function convert_date_php_js($date) {
    $converted_date = date("Y", strtotime($date)) . ', ' .
                      (date("n", strtotime($date))-1) . ', ' .
                      date("j", strtotime($date));
    return $converted_date;
}

$priceDate = "2016-09-14";
$d = convert_date_php_js($priceDate);
// Returns 2016, 8, 14



回答8:


It is very simple:

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


来源:https://stackoverflow.com/questions/9755911/send-php-date-to-javascript-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!