Jump to content

Help converting seconds to minutes and hours


edd12345678

Recommended Posts

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

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.