Jump to content

30 minute counter


herghost

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

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.