Jump to content

Coutdown Timer


HCProfessionals

Recommended Posts

I am currently in the process of creating an online time based game. One of the big things is that users get gold every half an hour :00 & :30. What I am trying to do is offer a physical clock so that users can see how much time is left before they more more gold. I have been trying to do this using PHP.

 

All I have currently is the time, but would like something like "$minutes until more gold".

 

This is currently what I have now: (added 1 hour for EST.)

<?php echo date("h:i A",time()+60*60); ?>

Link to comment
Share on other sites

Here is a PHP/Javascript countdown.  After the countdown expires, it resets on the next page load.  Modification would be required (shouldn't be hard).

 

<?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

Did you try it?  Just copy paste it into a file, name it with a php extension and see it work.  The only thing it will not do, is keep counting after the countdown expires.  If you think you would like something like this, I would see about modifying it for you.

Link to comment
Share on other sites

Well, I've been playing around with it and still can't get it.

 

Here's what i need it to do:

 

Count down from every half an hour server time - not when the user initiates it. For example, this is what I want it to say:

 

If it is 6:55, I want it say: "5 minutes until gold deposit"

 

If it is 5:22, It would say: "8 minutes left until gold deposit"

 

When the timer runs out, would it be possible for the script to refresh the page as well?

Link to comment
Share on other sites

OK, here you go.

 

Gold should be handled by the server.  I put it in the script just to show some gold on the page.  It will not increment, it will just display 100.

You should let the database handle the gold.

 

Timer runs on server time, and displays the message you want.  Until it gets to 1 minute, then it starts a countdown in seconds. Page refreshes on time hitting 0.

 

<?php
//session_start();

$gold = 100; //this amount should be handled and stored in the database.  Retrieved each time by the database.  

//MODIFICATION BELOW THIS LINE IS NOT REQUIRED
$date = date('i');
$sec = date('s');
$diff = ($date < 30) ? 1800 - (($date * 60) + $sec) : 3600 - (($date * 60) + $sec); //set diff to run split on the half hour.
$hld_diff = $diff;
/*
if(isset($_SESSION['ts'])) { //if timestamp is active in session
$slice = ($timestamp - $_SESSION['ts']);	 //get the difference between the timestamps.
$diff = $diff - $slice; //subtract the diff from the ts.
}

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" style="display:none">Clock Here!</div>
<div id="clock">Message 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 checkTime() {
var time = document.getElementById('strclock').innerHTML;
if(time == '00:00:00') {
window.location.reload(true);
}
}


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}$/;
secs = (pat.test(sec) == true) ? '0'+sec : sec;
mins = (pat.test(min) == true) ? '0'+min : min;
hours = (pat.test(hour) == true) ? '0'+hour : hour;

document.getElementById('strclock').innerHTML = hours+":"+mins+":"+secs;
if(min >= 1) {	
document.getElementById('clock').innerHTML = min+1+' minutes until gold deposit!';
}
else {
document.getElementById('clock').innerHTML = sec+' seconds until gold deposit!';
}

checkTime();
setTimeout("countdown()",1000);
}
countdown();
</script>

<table border="1">
<tr><th>GOLD</th></tr>
<tr><td><?php echo $gold; ?></td></tr>
</table>

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.