php - 如何以调用 PHP 文件的形式发送客户端 javascript 日期

标签 php jquery html

我使用 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/

相关文章:

javascript - Nivo Slider 转换无法正确呈现并增加网页长度

jquery - Superfish 子菜单超出屏幕边缘

html - 如何将文字放在封面图片下方?

javascript - 在 IE 和 Firefox 中设置 div 的最大高度和最大宽度

php - 在 PHP DOM 中获取节点的文本

php - MySQL 查询不起作用? (PHP)

javascript - 当 PHP 内联到 html(或其他 Web 文件)中时会发生什么?

javascript - 我想了解 indexOf

php - 如何检查文件是否正在使用,即正在对该文件中的数据执行某些命令?

PHP Bash 脚本调用另一个 Bash 脚本