duanchui1251 2018-08-09 13:06
浏览 66
已采纳

如何使用PHP显示计时器?

code:

<html>
    <head>
        <title>Timer</title>
    </head>
<body>
    <?php
        $dbSessionDuration = "1:30:00";
        list($hour,$min,$sec) = explode(':', $dbSessionDuration);
        $dbSessionDurationTime = mktime(0,0,0,$hour,$min,$sec);
    ?>
    <div class="count"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        var millis = <?php echo $dbSessionDurationTime; ?>
        function displaytimer(){
            var hours = Math.floor(millis / 36e5),
                mins = Math.floor((millis % 36e5) / 6e4),
                secs = Math.floor((millis % 6e4) / 1000);
                $('.count').html(hours+':'+mins+':'+secs);  
        }
        setInterval(function(){
            millis -= 1000;
            displaytimer();
        }, 1000);
    </script>
</body>
</html>

In this code I want to create a timer and the time will start after 1:30:00 and then it decrease by second like 1:29:59 then 1:29:58 like this but now when I use to get time inside the class="count" not happen. I don't know why? So, How can I fix this issue? please help me.

Thank You

  • 写回答

2条回答 默认 最新

  • dongtu7567 2018-08-09 13:31
    关注

    There is two notes on your code :-

    -If you want to convert a variable from php to javascript you should echo it inside double/single quote.

    -you need to get the total time in millis since you are using this millis to regenerate your time again so this is the equation to convert your time to millis ( ( intval($hour) * 60 * 60 ) + ( intval($min) * 60 ) + ( intval($sec) ) ) * 1000.

    Code

    <html>
    <head>
        <title>Timer</title>
    </head>
    <body>
        <?php
            $dbSessionDuration = "1:30:00";
            list($hour,$min,$sec) = explode(':', $dbSessionDuration);
            $dbSessionDurationTime =  ( ( intval($hour) * 60 * 60 ) + ( intval($min) * 60 ) + ( intval($sec) ) ) * 1000;
        ?>
        <div class="count"></div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script>
        <script type="text/javascript">
            var millis = '<?php echo $dbSessionDurationTime; ?>';
            function displaytimer(){
                var hours = Math.floor(millis / 36e5),
                    mins = Math.floor((millis % 36e5) / 6e4),
                secs = Math.floor((millis % 6e4) / 1000);
                $('.count').html(hours+':'+mins+':'+secs);  
            }
            setInterval(function(){
                millis -= 1000;
                displaytimer();
            }, 1000);
        </script>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊