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条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!