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
https://forums.phpfreaks.com/topic/180116-30-minute-counter/
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
https://forums.phpfreaks.com/topic/180116-30-minute-counter/#findComment-950290
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
https://forums.phpfreaks.com/topic/180116-30-minute-counter/#findComment-950888
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.