edd12345678 Posted January 27, 2012 Share Posted January 27, 2012 Hi, I wonder if someone could possibly help me. I have a countdown timer which will count down from a value in my SQL database. However it count downs in seconds and I need it to show in format hh:mm:ss. I have tried using: <?php echo gmdate("H:i:s", $timeTilEnd);?> Which starts it off but soon as it counts down one second goes back to just showing the seonds. My full code for the timer is: <?php //Start of Timer Function if (!isset($_SESSION['endOfTimer'])){ $endOfTimer = time() + $time * 60; //multiplys the time in db by 60 to create second countdown. $_SESSION['endOfTimer'] = $endOfTimer; } if(($_SESSION['endOfTimer'] - time()) < 0) { $timeTilEnd = 0; } else { $timeTilEnd = $_SESSION['endOfTimer'] - time(); } if($timeTilEnd <= 0) { session_destroy(); } ?> <script type="text/javascript"> var TimeLeft = <?php echo $timeTilEnd; ?>; function countdown() { if(TimeLeft > 0) { TimeLeft -= 1; document.getElementById('timer').innerHTML = TimeLeft; } if(TimeLeft <= 60) { //add text here to change color of message when in the last minute } if(TimeLeft <= 0) { window.location = "Test-Finished.php" } } CountFunc = setInterval(countdown,1000); </script> <?php echo gmdate("H:i:s", $timeTilEnd);?> Has anyone got any ideas on how I can do this, Its driving me mad. Thanks in advance. Edd Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/ Share on other sites More sharing options...
php.ajax.coder Posted January 27, 2012 Share Posted January 27, 2012 Ran in to this problem a few days ago here's the code i use $time = the number of seconds <?php function format_time($time){ $seconds = $time % 60; $time = ($time - $seconds) / 60; $minutes = $time % 60; $hours = ($time - $minutes) / 60; //Format Number return sprintf('%02d', $hours) . ":" . sprintf('%02d', $minutes) . ":" . sprintf('%02d', $seconds); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1311835 Share on other sites More sharing options...
lonewolf217 Posted January 27, 2012 Share Posted January 27, 2012 format your javascript output to show the time properly document.getElementById('timer').innerHTML = (Math.floor(TimeLeft/60) + " minutes and " + (TimeLeft%60) + " seconds"); Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1311836 Share on other sites More sharing options...
edd12345678 Posted January 28, 2012 Author Share Posted January 28, 2012 Hi, Many thanks for both of your replies. I have implemented both and they do work. However they do not countdown like my timer did before. The time left is only displayed when the page is refreshed. Is there away to use the code you suggested and have it so it counts down the time? Thanks in advance. Edd Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1311919 Share on other sites More sharing options...
lonewolf217 Posted January 28, 2012 Share Posted January 28, 2012 this works, you can adapt it to your need <span id="TIME"></span> <Script type="text/javascript"> var TimeLeft = 100; function countdown() { if(TimeLeft > 0) { TimeLeft -= 1; document.getElementById("TIME").innerHTML =(Math.floor(TimeLeft/60) + " minutes and " + (TimeLeft%60) + " seconds"); } if(TimeLeft <= 60) { //add text here to change color of message when in the last minute } if(TimeLeft <= 0) { document.write("DONE!"); return false; } t=setTimeout("countdown()",1000); } countdown(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1311940 Share on other sites More sharing options...
edd12345678 Posted January 29, 2012 Author Share Posted January 29, 2012 Thankyou thats just what I needed! Is it easy to set it so it works out the hours as well? Thanks again Ed Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1312156 Share on other sites More sharing options...
lonewolf217 Posted January 29, 2012 Share Posted January 29, 2012 you can easily modify the math to show whatever you want Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1312163 Share on other sites More sharing options...
edd12345678 Posted January 29, 2012 Author Share Posted January 29, 2012 Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/255911-help-converting-seconds-to-minutes-and-hours/#findComment-1312359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.