MasterACE14 Posted March 7, 2009 Share Posted March 7, 2009 hey, I have a column which is updated with time(); in the database every 30mins. I want to find out how many minutes and seconds are between now and then. What I have in the moment is close, but not quite right. <?php $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins $currentTime = time(); // current time $nextTurnMin = date("i",($currentTime)) - date("i",round($lastTurn)); // work out the difference any help is greatly appreciated Regards ACE Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 7, 2009 Author Share Posted March 7, 2009 forgot to mention $currentTime and $lastTurn are being rounded otherwise date(); doesn't like it when I compare them in seconds. Quote Link to comment Share on other sites More sharing options...
MadnessRed Posted March 7, 2009 Share Posted March 7, 2009 $diff = time()-LastTurnTime($userrow['ID']); $secs = ($diff % 60); $mins = ($diff - $secs) / 60; how about that? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 7, 2009 Author Share Posted March 7, 2009 it's soughta working lol. This is a countdown timer until my cron which will update LastTurnTime. so I have this in the moment: <?php $diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin); // $nextTurnMin is how many minutes till the next update(which happen every half an hour) $mins = ($diff % 60); $secs = ($diff - $mins); been experimenting for hours trying to get this right. I'm basically making the count down livein javascript, which is working. Just the current Server Time, doesn't match up with the current count down. Like the closest I have got it is within 2 seconds of being the same. I have... <?php print date("M j, H:i:s", ($currentTime)); // $currentTime is time(); , this is also a block of javascript ?> which is showing the current server time. and this is showing the countdown... <script language="javascript"> <!-- var sec = <?php echo $secs; ?>; // set the seconds var min = <?php echo $mins; ?>; // set the minutes function countDown() { sec--; if (sec == -01) { sec = 59; min = min - 1; } else { min = min; } if (sec<=9) { sec = "0" + sec; } time = (min<=9 ? "0" + min : min) + ":" + sec + " min "; if (document.getElementById) { theTime.innerHTML = time; } SD=window.setTimeout("countDown();", 1000); if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); time = "Now!"; } } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } --> </script> in the moment the countdown timer is looking like this... 0-22:43 min until next Update while the server time is about 4 minutes ahead. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Would be tempted to do it in the MySQL call that gets the last time updated. I presume LastTurnTime is an array populates from a datetime field on the database. If so use SELECT (TIMESTAMPDIFF(SECOND, NOW(), LastTurnTime) as TimeSinceLastTurnTime to get the difference in seconds. All the best Keith Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 7, 2009 Author Share Posted March 7, 2009 the column is int(11) for lastTurnTime :S Quote Link to comment Share on other sites More sharing options...
MadnessRed Posted March 7, 2009 Share Posted March 7, 2009 here is my javascript countdown script. the things in {} need setting to what you want. <script type="text/javascript"> function countdown{cid}(){ document.getElementById('countdown{cid}').innerHTML = ((document.getElementById('countdown{cid}').innerHTML * 1) - 1); if(document.getElementById('countdown{cid}').innerHTML > 0){ var s = document.getElementById('countdown{cid}').innerHTML % 60; var m = ((document.getElementById('countdown{cid}').innerHTML - s) / 60) % 60; var h = ((((document.getElementById('countdown{cid}').innerHTML - s) / 60) - m) / 60) % 24; var d = ((((((document.getElementById('countdown{cid}').innerHTML - s) / 60) - m) / 60) - h) / 24); var x = ''; if( d > 0 ){ x = x + " "+d+"d"; } if( h > 0 ){ x = x + " "+h+"h"; } if( m > 0 ){ x = x + " "+m+"m"; } if( s > 0 ){ x = x + " "+s+"s"; } }else{ var x = '{cfin}'; } document.getElementById('countdown{cid}p').innerHTML = x; var t=setTimeout("countdown{cid}()",1000); } countdown{cid}(); </script> <div id="countdown{cid}" style="display:none;">{cstart}</div><span id="countdown{cid}p"></span> {cid} is the id of the countdown, it is if you have more than 1 countdown on the page. {cstart} is how many seconds there are {cfin} is what to say when the time has been reached. you should add into the body <body onload="countdown{cid}();"> Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Ah, so not a datetime field. Oh well. What format is it in? All the best Keith Quote Link to comment Share on other sites More sharing options...
MadnessRed Posted March 7, 2009 Share Posted March 7, 2009 Ah, so not a datetime field. Oh well. What format is it in? All the best Keith the column is int(11) for lastTurnTime :S Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 7, 2009 Author Share Posted March 7, 2009 I got nothing coming up now lol. Think I did something wrong? <?php $diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin); $mins = ($diff % 60); $NextTurn = $mins * 60; ?> <script type="text/javascript"> function countdown1() { document.getElementById('countdown1').innerHTML = ((document.getElementById('countdown1').innerHTML * 1) - 1); if(document.getElementById('countdown1').innerHTML > 0){ var s = document.getElementById('countdown1').innerHTML % 60; var m = ((document.getElementById('countdown1').innerHTML - s) / 60) % 60; var h = ((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) % 24; var d = ((((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) - h) / 24); var x = ''; if( d > 0 ){ x = x + " "+d+"d"; } if( h > 0 ){ x = x + " "+h+"h"; } if( m > 0 ){ x = x + " "+m+"m"; } if( s > 0 ){ x = x + " "+s+"s"; } }else{ var x = 'Now!'; } document.getElementById('countdown1p').innerHTML = x; var t=setTimeout("countdown1()",1000); } countdown1(); </script> <div id="countdown1" style="display:none;"><?php echo $NextTurn; ?></div><span id="countdown1p"></span> Ah, so not a datetime field. Oh well. What format is it in? All the best Keith it's integer, I insert the time(); function into it with PHP. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Right, you are storing a number of seconds (ie, could have been minutes). Doing it in php:- $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins $currentTime = time(); // current time $nextTurnSecs = (time() - $lastTurndate); // work out the difference $nextTurnMin = floor($nextTurnSecs/60); // work out the difference $nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference Not tried it but think that will work and give you the minutes and seconds since the time, in a pair of fields. All the best Keith Quote Link to comment Share on other sites More sharing options...
MadnessRed Posted March 7, 2009 Share Posted March 7, 2009 I got nothing coming up now lol. Think I did something wrong? <?php $diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin); $mins = ($diff % 60); $NextTurn = $mins * 60; ?> <script type="text/javascript"> function countdown1() { document.getElementById('countdown1').innerHTML = ((document.getElementById('countdown1').innerHTML * 1) - 1); if(document.getElementById('countdown1').innerHTML > 0){ var s = document.getElementById('countdown1').innerHTML % 60; var m = ((document.getElementById('countdown1').innerHTML - s) / 60) % 60; var h = ((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) % 24; var d = ((((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) - h) / 24); var x = ''; if( d > 0 ){ x = x + " "+d+"d"; } if( h > 0 ){ x = x + " "+h+"h"; } if( m > 0 ){ x = x + " "+m+"m"; } if( s > 0 ){ x = x + " "+s+"s"; } }else{ var x = 'Now!'; } document.getElementById('countdown1p').innerHTML = x; var t=setTimeout("countdown1()",1000); } countdown1(); </script> <div id="countdown1" style="display:none;"><?php echo $NextTurn; ?></div><span id="countdown1p"></span> Ah, so not a datetime field. Oh well. What format is it in? All the best Keith it's integer, I insert the time(); function into it with PHP. $diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin); why are you ceil'ing an integer? ciel rounds up to the next integer, time() is already an integer? also what exactly are you trying to do? Your script is using variables and you aren't defining them. What do you want the time until and how is it worked out? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 7, 2009 Author Share Posted March 7, 2009 <?php $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins $currentTime = time(); // current time $nextTurnSecs = (time() - $lastTurndate); // work out the difference $nextTurnMin = floor($nextTurnSecs/60); // work out the difference $nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference ?> <script language="javascript"> <!-- var sec = <?php echo $nextTurnMin; ?>; // set the seconds var min = <?php echo $nextTurnSecs; ?>; // set the minutes function countDown() { sec--; if (sec == -01) { sec = 59; min = min - 1; } else { min = min; } if (sec<=9) { sec = "0" + sec; } time = (min<=9 ? "0" + min : min) + ":" + sec + " min "; if (document.getElementById) { theTime.innerHTML = time; } SD=window.setTimeout("countDown();", 1000); if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); time = "Now!"; } } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } --> </script> <span id="theTime" class="timeClass"></span> That javascripts working. But the time until Next Turn is showing up as... 52:6316436 <<< time(); I believe Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 8, 2009 Author Share Posted March 8, 2009 bump Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Had a play and think I have got it to work:- <?php $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins $currentTime = time(); // current time $nextTurnSecs = 1800 - (($currentTime - $lastTurn)); // work out the difference $nextTurnMin = floor($nextTurnSecs/60); // work out the difference $nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference ?> <script language="javascript"> <!-- var sec = <?php echo $nextTurnSecs; ?>; // set the seconds var min = <?php echo $nextTurnMin; ?>; // set the minutes addLoadEvent = countDown(); function countDown() { sec--; if (sec == -01) { sec = 59; min = min - 1; } else { min = min; } if (sec<=9) { sec = "0" + sec; } var time = (min<=9 ? "0" + min : min) + ":" + sec + " min "; if (document.getElementById('$currentTime')) { document.getElementById('$currentTime').innerHTML = time; } SD=window.setTimeout("countDown();", 1000); if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); time = "Now!"; } } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } --> </script> Primary error was the Javascript minutes and seconds being the wrong way round. Also the calculation at the top calculated the number of seconds since last updated, while you wanted to count down from that until 30 minutes later (hence now taking the number of seconds since last updated away from 1800). All the best Keith Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 13, 2009 Author Share Posted March 13, 2009 How do I call the javascript now? because <span id="theTime" class="timeClass"></span> isn't working anymore :-/ Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 13, 2009 Share Posted March 13, 2009 Hi It is the line addLoadEvent = countDown(); that is there to call the Javascript. The span is merely where the Javascript puts the result. All the best Keith 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.