我使用 PHP 将日期和时间写入 MySql 表。我想写客户端日期时间和服务器日期时间。我用 PHP 计算出服务器日期/时间,所以我只需要将客户端日期/时间存储在 PHP 变量中。感谢您的任何输入。我觉得我真的很亲密。在我的 isset 中有一件奇怪的事情。我声明如果为空则变量 =“空”,但即使 strlen 为 0,它也不会显示“空”
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function ShowLocalDate()
{
var dNow = new Date();
var localdate= (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
document.getElementById("createdAt").innerHTML = localdate;
}
</script>
</head>
<body onload="ShowLocalDate()">
<h1>Get current local Date in JQuery</h1>
<label id="createdAt">This is current local Date Time in JQuery</label>
<form action="date_time_tool2.php" method="post">
<input type="hidden" id="createdAt" name="java_to_php" />
<button type="submit" >X-fer Date</button>
</form>
</body>
</body>
</html>
date_time_tool2.php
<?php
$php_client_date = isset($_REQUEST['java_to_php']) ? $_REQUEST['java_to_php'] : "empty";
echo 'Date = ' . $php_client_date . ' length = ' . strlen($php_client_date);
?>
最佳答案
使用 .value
而不是 .innerHTML
所以现在你的
function ShowLocalDate()
{
var dNow = new Date();
var localdate= (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
document.getElementById("createdAtInput").value = localdate;
// For JQuery you can use it like this
$("#createdAtJquery").val(localdate);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="ShowLocalDate()">
<h1>Get current local Date in JQuery</h1>
<label id="createdAt">This is current local Date Time in JQuery</label>
<form action="date_time_tool2.php" method="post">
<input type="text" id="createdAtInput" name="java_to_php" />
<input type="text" id="createdAtJquery" name="java_to_php" />
<button type="submit" >X-fer Date</button>
</form>
</body>
I have changed the type from hidden to text for you to see. And the important thing is you can not use the same id in your HTML more than once.So please consider taking some javascript course as there are millions of them and most being free.
关于php - 如何以调用 PHP 文件的形式发送客户端 javascript 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472179/