简体   繁体   English

将PHP倒数计时转换为JavaScript

[英]Turn php countdown into javascript

On my php website I have many various countdowns. 在我的php网站上,我有许多倒数计时。 These are only updated when the user refreshes the page. 这些仅在用户刷新页面时更新。 I want to change it so that the countdown is continous. 我想更改它以便倒数计时连续进行。

I have found many javascript codes that perform this, however I'm unsure how to implement my php code into there script. 我发现许多执行此操作的javascript代码,但是我不确定如何将我的php代码实现到该脚本中。

Please see below scripts: 请参见以下脚本:

My php function for timer: 我的计时器的PHP功能:

function countup ($online){
global $time;

$difference=$online-$time;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);

if($days != 0){echo"$days days, ";}
if($hours != 0){echo"$hours hours, ";}
if($mins != 0){echo"$mins minutes and ";}
echo"$secs seconds";
}

where I show the timer I have the following.. <?=countup($set[ends])?> 我在哪里显示计时器,我有以下内容。 <?=countup($set[ends])?>

Ends = future unix timestamp. 结束=将来的Unix时间戳。

The javascript countdown I came across was from http://keith-wood.name/countdown.html but I have no understanding of java and am not sure how to put $set[ends] into it! 我遇到的JavaScript倒计时来自http://keith-wood.name/countdown.html,但我对Java不了解,因此不确定如何将$ set [ends]放入其中!

All help would be greatly appreciated! 所有帮助将不胜感激!

Considering, that you're loading all required scripts properly, here is what I would suggest: 考虑到您正在正确加载所有必需的脚本,这是我的建议:

PHP part PHP部分

$ends = date("Y, n-1, j", $set[ends]); //covert your time stamp to the required format

**JavaScript Part ** ** JavaScript部分**

<script type='text/javascript'>
  ( function( $ ) {
      $('#yourCountDownDIV').countdown({until: <?php echo $ends ?>});
  } )( jQuery );
</script>

Put the JavaScript part at the end of the HTML code just before the closing tag and after all your JavaScript files are loaded. 将JavaScript部分放在HTML代码的末尾,紧接在结束标记之前,然后加载所有JavaScript文件。

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

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