简体   繁体   English

如何使用PHP单击按钮后获得时间

[英]How to get Time After button is clicked with PHP

What I want to do is get the Time of the server, the moment I click a button. 我想做的就是获取服务器的时间,即单击按钮的那一刻。 This time needs to be calculated(retrieved) at the time of click, this needs to be able to be done repeatedly if needed. 此时间需要在单击时进行计算(检索),如果需要的话,需要能够重复进行。 Currently I have tried: 目前,我已经尝试过:
PHP Code PHP代码

<?php
function getTime(){
    $Time = date('m/d/Y h:i:s a', time());
    return $Time;
}
?>

JS Code JS代码

function Funct(){ var Time = "<?php echo json_encode(getTime()); ?>"; console.log(Time); }
HTML Code HTML代码
<button onclick="Funct()">Get Time</button>
The problem is that when I click the button, after the first click, all of the other clicks Will not update, in fact the getTime function seems to be called when the page loads and leaves a string literal that is logged. 问题是,当我单击按钮时,第一次单击后,所有其他单击都将不会更新,实际上,当页面加载并留下记录的字符串文字时,getTime函数似乎已被调用。 How can I control when it calculates the Time? 如何控制其计算时间? Thanks for the help! 谢谢您的帮助!

Try this : 尝试这个 :

HTML HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>    
<button onclick="Funct()">Get Time</button>

JS JS

    function Funct(){
         var data = {};
         $.ajax({ type: 'POST', url: 'php_file.php', data: data, dataType: 'json', encode: true })
        .done(function (data) {
          console.log(data.time);
        }).fail(function (data) {
           console.log(data.responseText);
         });
    }

PHP PHP

    <?php 
        $data['time'] = date('m/d/Y h:i:s a', time());
        echo json_encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM