Please consider the following scenario with a simple HTML document and a JavaScript loaded in it:
<!-- doc.html -->
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
</html>
// script.js
window.onload = function()
{
var d = new Date();
d.setHours(<?php echo(date("H")); ?>);
}
It's really Quite simple, isn't it?
However, while this coode works perfectly for getting the current hour (which will be returned as an INT
), it doesn't if I try to receive a more complex date/time format like the one below:
var serverTime = <?php echo date("Y-m-d H:i:s"); ?>;
This should encode to 1900-01-01 00:00:00
. So, cause this is not a legal expression I want to encapsulate the result with quotes:
var serverTime = "<?php echo date("Y-m-d H:i:s"); ?>";
At a first glance this should work. Unfortunately, it doesn't. serverTime
contains just an empty string.
What am I doing wrong?
echo date("Y-m-d H:i:s");
in PHP?var d = Date(); d.setTime(<?php echo time() * 1000; ?>);
1900-01-01 00:00:00
, it would show the current time and date. PHP's date does not even go back that far!