Jump to content

simple PHP based countdow timer


jacko_162

Recommended Posts

When it comes to php im still no expert,

 

and when it comes to date/time within PHP my brain melts...

 

what i want is a countdown timer in php that echos how long left is on a timer.

 

i will set the timer for 60 minutes.

 

and i want it to countdown in minutes (no need to display hours, seconds etc...)

 

everytime the user refreshes the page the coundown will obivously refresh.

 

how can i code this or anyone have any handy links to point me in the right direction.

 

Thanks

Link to comment
Share on other sites

and i need to format the output something like:

 

"Time remaining until next run is in 59 Minutes"

 

not sure how i can set it around but its basically working of a cron script that runs every 10 minutes past the hour. thats primarily what the timer is to show users when the next "update" will be.

Link to comment
Share on other sites

There is a simple solution for this, which I will not delve into greatly.

 

You need to use the PHP time() function, this will give you the time since 1970 [since Epoch] for example 1234567890+3600 [time+60minutes], then you will need to save this in a file/database whatever.

When you can saved it, anytime you need to timer you will call the record/file then compare with the current time() then minus this from the savd time to give you the seconds till the saved time.

 

Then do some simple math to calulate how much time till the saved time from the current time.

 

Example

----------------------

<?PHP
$saveTime = (1234567890+3600); // Saved time from file/database
$thisTime = time(); // Current time
$diffTime = (saveTime-thisTime); // Difference in time

  if($diffTime >= 1) {
    $countMin = floor(diffTime/60);
    $countSec = (diffTime-(countMin*60));
    echo 'Time remaining until next run is in ',$countMin,' minute(s) ',$countSec,' seconds';
  } else {
    echo 'Timer expired.';
  }
?>

 

That should get you on your way :)

 

Regards, PaulRyan.

 

Link to comment
Share on other sites

This countdown includes hours and seconds, incorporates PHP and Javascript.  It may not be exactly as you want it, but it shows how you can hold the count between page refreshes.

<?php
session_start();
$timestamp = time();
$diff = 30; //<-Time of countdown in seconds.  ie. 3600 = 1 hr. or 86400 = 1 day.

//MODIFICATION BELOW THIS LINE IS NOT REQUIRED.
$hld_diff = $diff;
if(isset($_SESSION['ts'])) {
$slice = ($timestamp - $_SESSION['ts']);	
$diff = $diff - $slice;
}

if(!isset($_SESSION['ts']) || $diff > $hld_diff || $diff < 0) {
$diff = $hld_diff;
$_SESSION['ts'] = $timestamp;
}

//Below is demonstration of output.  Seconds could be passed to Javascript.
$diff; //$diff holds seconds less than 3600 (1 hour);

$hours = floor($diff / 3600) . ' : ';
$diff = $diff % 3600;
$minutes = floor($diff / 60) . ' : ';
$diff = $diff % 60;
$seconds = $diff;


?>
<div id="strclock">Clock Here!</div>
<script type="text/javascript">
var hour = <?php echo floor($hours); ?>;
var min = <?php echo floor($minutes); ?>;
var sec = <?php echo floor($seconds); ?>

function countdown() {
if(sec <= 0 && min > 0) {
  sec = 59;
  min -= 1;
}
else if(min <= 0 && sec <= 0) {
  min = 0;
  sec = 0;
}
else {
  sec -= 1;
}

if(min <= 0 && hour > 0) {
  min = 59;
  hour -= 1;
}

var pat = /^[0-9]{1}$/;
sec = (pat.test(sec) == true) ? '0'+sec : sec;
min = (pat.test(min) == true) ? '0'+min : min;
hour = (pat.test(hour) == true) ? '0'+hour : hour;

document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec;
setTimeout("countdown()",1000);
}
countdown();
</script>

Link to comment
Share on other sites

ok this is my test.php page:

 

<?php
include "connect.php";

// GRAB LAST UPDATE TIME OF CRON FROM DATABASE
$queryTime="SELECT time FROM cron WHERE id='1'";

$result=mysql_query($queryTime);
while($row=mysql_fetch_assoc($result))
  $setTime = $row['time'];
    
//echo "$setTime"; // ECHO THE INTERGER TIME FOR DEBUG
//echo "<br>";
$Time = time(); // Sets time in interger
//echo $Time;
$timePlus = time()+3600; // Sets time in interger and adds 1 hour to match with UTC Time
//echo $timePlus;
$Date = date("Ymd"); // Sets date in 20100212 format

$lastrun = date("H:i", $setTime); // converts last run from integer to XX:xx
//echo $lastrun;
$lastrun2 = date($setTime)-3600; // server is 1 hour in front so take 1 hour away
//echo $lastrun2;
$lastrun3 = date("H:i", $lastrun2); // converts last run minus 1 hour from integer to XX:xx
//echo $lastrun3;

//now you want to figure out when next run is due (server is 1 hour behing UTC so you need to + 3600)
$nextrun = $lastrun2 + 3600; // calculate when next run is due and adds 1 hour to match with UTC Time.
$nextruntime = date("H:i", $nextrun); //converts next run from integer to xx:xx

//now you want to calculate the time now "UTC" and countdown from the next due time
$countdown =  $nextrun - $timePlus;
$countdownmins = round($countdown/60, 2);

echo "last cron was at $lastrun3, the next cron will be at $nextruntime, the time remaining until next cron is in $countdownmins Minutes";

?>

 

i got the cron file to insert the time() in the database that it ran, that works perfectly now. runs on the hour.

 

as you can see the above 90% works except the last section:

 

//now you want to calculate the time now "UTC" and countdown from the next due time
$countdown =  $nextrun - $timePlus;
$countdownmins = round($countdown/60, 2);

echo "last cron was at $lastrun3, the next cron will be at $nextruntime, the time remaining until next cron is in $countdownmins Minutes";

 

its not calculating the minutes correctly and counting them down.

 

here is link to my current page:

http://www.shiplotto.com/test.php

 

thank you for help thus far.. i would appreciate if someone could help me finish up this code before my brain pours out of my ears on the floor in a php math mess..

Link to comment
Share on other sites

The following will work, and is much less code :)

<?php

  date_default_timezone_set('Europe/London'); // Set your default timezone

  include ('connect.php');

  $myQuery = mysql_query("SELECT time FROM cron WHERE id = 1");

  while($result = mysql_fetch_assoc($myQuery)) {
    $setTime = $result['time'];

    //echo $setTime,'<br>'; // For debugging purposes
    
    $lastRun = date("H:i", $setTime);         // Set the last run time
    $nextRun = date("H:i", $setTime+3600);    // Set the next run time

    $timeDiff = (($setTime-time())+3600);     // Calculate time difference
    $countMins = floor($timeDiff/60);         // Count the number of minutes
    $countSecs = ($timeDiff-($countMins*60)); // Count the number of seconds

    echo 'Last Cronjob was run at ',$lastRun,', it will run again in ',$countMins,' minutes and ',$countSecs,' seconds at ',$nextRun,'';

  }
?>

 

Tell me how it goes bud :)

 

Regards, PaulRyan.

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.