herghost Posted November 3, 2009 Share Posted November 3, 2009 Hi all, I have a cron job that runs every 30 mins (half past and on the hour) What I want is a countdown timer that counts down each half hour before the cron runs. However I have absoloutly no javascript experience, none at all! Where would I start with this or does anyone know of a script that will do it? Many Thanks Quote Link to comment Share on other sites More sharing options...
Adam Posted November 3, 2009 Share Posted November 3, 2009 I actually had this laying around (minus a few modifications to fit your needs). It's really simply (and a bit hacky to be honest), but you should be able to use it as a starting point... <?php $mins = date("i"); if ($mins >= 0 && $mins < 30) { $timestr = date("H").':30'; } else { $hour = (date("H") == 23) ? 00 : date("H")+1; $timestr = $hour.':00'; } $timeleft = strtotime($timestr) - time(); ?> <script type="text/javascript"> var timeleft = <?php print $timeleft; ?>; window.onload = function() { timer = setInterval(function() { timeleft = (timeleft == 0) ? 1800 : timeleft-1; document.getElementById('timeleft').innerHTML = timeleft; }, 1000); } </script> <span id="timeleft"><?php echo $timeleft; ?></span> seconds left Edit: corrected a bug in the code. Quote Link to comment Share on other sites More sharing options...
herghost Posted November 4, 2009 Author Share Posted November 4, 2009 Thanks! How would I edit this to display time like 28:05 left? Quote Link to comment Share on other sites More sharing options...
Adam Posted November 4, 2009 Share Posted November 4, 2009 It's probs easier than you think.. <?php $mins = date("i"); if ($mins >= 0 && $mins < 30) { $timestr = date("H").':30'; } else { $hour = (date("H") == 23) ? 00 : date("H")+1; $timestr = $hour.':00'; } $timeleft = strtotime($timestr) - time(); ?> <script type="text/javascript"> var timeleft = <?php print $timeleft; ?>; window.onload = function() { timer = setInterval(function() { timeleft = (timeleft == 0) ? 1800 : timeleft-1; document.getElementById('timeleft').innerHTML = Math.floor(timeleft / 60) + ':' + Math.floor(timeleft % 60); }, 1000); } </script> <span id="timeleft"><?php echo floor($timeleft / 60) . ':' . floor($timeleft % 60); ?></span> 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.