[英]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>
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.