Hey guys, sorry for the oldtimes engine spam questions, i really want to make this one of the best engines for indie developers!
This time i come here saying that the building system is 100% done and fully operational, added some functions and stuff. Now i need to know, how can i make a easy to add countdown timer so it refreshes every second and when its done it does a refresh on the page.
I searched on google and found out this method:
http://stackoverflow.com/questions/1191 ... down-timer
But i cant seem to find a way to implement with the code.
The code im using is the time() function, and check if the time() >= $building_time //building time is the time when the build is finished.
So i want a function (the timer) to be able to get the difference between those two values and display a timer and countdown till 0, then i just need a refresh and the building system will do the rest.
Thanks for the help.
Whoever helps the dev of this engine gets a special spot in the credits, indie-resource.com is granted, but you can add your name to it.
[PHP] Countdown Timer
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: [PHP] Countdown Timer
I got this code originally I think from Alexander on these forums (runs the Nryis pbbg, I think?). I refined it slightly:
I use it with an include statement, setting its values just prior: $countdown for the time in seconds, $newpage for the page to go to. The other values you can set are $endcountermessage and $fontsize are optional.
I use it with an include statement, setting its values just prior: $countdown for the time in seconds, $newpage for the page to go to. The other values you can set are $endcountermessage and $fontsize are optional.
Code: Select all
<?php
// uses $newpage for the page to go to after the count completes, if any
if (!isset($newpage)) $newpage="null"; // for when there is no page jump
if (!isset($endcountmessage)) $endcountmessage="---";
if (!isset($fontsize)) $fontsize=26;
if($countdown > 0)
{
// closing of PHP here so as to do the javascript
?>
<span id='time' style='text-align:left;font-size:<?php echo $fontsize; ?>px; color:#300; font-weight:bold'></span>
<script type='text/javascript'>
v=new Date();
var time=document.getElementById('time');
function date(){
n=new Date();
//here we insert the time
seconds=<?php echo $countdown; ?>-Math.round((n.getTime()-v.getTime())/1000.);
minutes=0;
hours=0;
if(seconds<0)
{
time.innerHTML='<?php echo $endcountmessage; ?>'; //'---';
// uncomment the line below to enable redirect at the end of the countdown
<?php if ($newpage<>"null") echo "document.location='".$newpage."';" ?>
//document.location='wurld.php';
}
else
{
if(seconds>59)
{
minutes=Math.floor(seconds/60);
seconds=seconds-minutes*60
}
if(minutes>59)
{
hours=Math.floor(minutes/60);
minutes=minutes-hours*60
}
if(seconds<10)
{
seconds='0'+seconds
}
if(minutes<10)
{
minutes='0'+minutes
}
time.innerHTML=' '+hours+':'+minutes+':'+seconds+'';
document.title=hours+':'+minutes+':'+seconds;
window.setTimeout('date();',999);
}
}
date();
</script>
<?php
} // end of if $countdown>0 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
?>
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: [PHP] Countdown Timer
You said you use it with an include statement.. so i works like a function and i can just include it in the page i need the countdown.
But first i need to declare $countdown and $newpage etc right?
Also in the code i need to add for the $newpage to work a else on if($countdown > 0) so it goes to the $newpage. Right?
But first i need to declare $countdown and $newpage etc right?
Also in the code i need to add for the $newpage to work a else on if($countdown > 0) so it goes to the $newpage. Right?
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: [PHP] Countdown Timer
EDIT: IT REALLY WORKS BETTER THAN I EXPECTED! THANKS. ADDED TO THE CREDITS, AS SO ALEXANDER.