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

如何使用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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujiu6976 2018-08-09 13:33

    The problem is at your convertion, it's returning 949219200 where in fact it should have returned 5400000

    <html>
        <head>
            <title>Timer</title>
        </head>
    <body>
        <?php
          $dbSessionDuration = "1:30:00";
    
          $time   = explode(":", $dbSessionDuration);
    
          $hour   = $time[0] * 60 * 60 * 1000;
          $minute = $time[1] * 60 * 1000;
          $sec    = $time[2] * 1000;
    
          $dbSessionDurationTime = $hour + $minute + $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>
    

    Now the $dbSessionDurationTime will contain the right number of milisecs of 5400000 Here is a working codepen: https://codepen.io/anon/pen/EpGYKo of course the php part its not there but you can see the timer working as you want it to.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题