bachx Posted September 6, 2007 Share Posted September 6, 2007 I have a a UNIX format number that I want to convert into an Hours:Minutes:Seconds format for a stopwatch/countdown timer script I'm writing. I used date("H:i:s", ($time)), but the problem is, if the period I'm counting is above 24 hours, the Hours reset to 0. So If I was counting down 49 Hours, It's be displayed as 01:00:00 instead of 49:00:00. Any help? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 6, 2007 Share Posted September 6, 2007 You'll need to figure out how many hours, minutes and seconds are left manually, using math. Quote Link to comment Share on other sites More sharing options...
bachx Posted September 6, 2007 Author Share Posted September 6, 2007 I already have the total number of seconds (in unix format) for the time left, I just need to convert it into H:i:s format without it resetting every 24 hours. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 6, 2007 Share Posted September 6, 2007 You'll need to figure out how many hours, minutes and seconds are left manually, using math. Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 6, 2007 Share Posted September 6, 2007 Post your current script and I'll do it for you. Quote Link to comment Share on other sites More sharing options...
bachx Posted September 6, 2007 Author Share Posted September 6, 2007 Thanks. There is no 'script', I simply have a variable called $time which has the remaining time in seconds. I know dividing that on 3600 returns the number of hours. What about minutes/seconds? Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 6, 2007 Share Posted September 6, 2007 <?php $t = htmlentities($_GET['t']); $time['days'] = (int) ($t / (60 * 60 * 24)); $t = $t % (60 * 60 * 24); $time['hours'] = (int) ($t / (60 * 60)); $t = $t % (60 * 60); $time['minutes'] = (int) ($t / 60); $time['seconds'] = $t % 60; // output style 1 foreach ($time as $key => $value) { if (!empty($value)) { echo "$value $key "; } } // output style 1 end echo '<br />'; // output style 2 $time['hours'] += $time['days'] * 24; foreach ($time as $key => $value) { if (strlen($time[$key]) == 1) { $time[$key] = '0' . $value; } } echo $time['hours'] . ':' . $time['minutes'] . ':' . $time['seconds']; // output style 2 end ?> http://sneamia.net/junk/bachx/?t=100 Quote Link to comment Share on other sites More sharing options...
bachx Posted September 7, 2007 Author Share Posted September 7, 2007 Awesome, thanks. Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 7, 2007 Share Posted September 7, 2007 Remember to hit topic solved. Quote Link to comment 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.