# Countdown in PHP



## Lazerbozz

Hi!

I've been wondering about how to create a countdown in PHP.

It should count down fram 200 sec to 0, but if you leave the page it should countinue counting while you are not watching. And when it is done counting down it should not start againg before you enter the site again.

Sorry if its differcould to understand me, but i'm Norwegian.


----------



## Lazerbozz

#! /bin/sh
echo 200
if [ -n "$x" ]
count=$x
else
count=10
fi

while [ $count -ge 0 ]

do
echo $count seconds
count=`expr $count - 1`
done
echo Off we go! 

Will this do the trick?


----------



## tajjyarden

In order to have an active countdown you will need to use DHTML. You can have a PHP countdown, but the visual countdown will only update when the page is refreshed. Are you wanting a countdown that visually counts down when you're looking at it?

Basically, you could use php to set a cookie or session variable that it a timestamp of when you want the timer to reach zero. So if you want a 200 second countdown, you set the timestamp as 200 seconds from now:

Initialize the timer once


Code:


<?php
session_start();
if (!isset($_SESSION['endOfTimer'])){
    $endOfTimer = time() + 200;
    $_SESSION['endOfTimer'] = $endOfTimer;
} else {
    $timeTilEnd = $_SESSION['endOfTimer'] - time();
    echo 'You have '.$timeTilEnd.' seconds left.';
}
?>


----------



## Pyro-Fire

fixed tajjyarden's code, and added javascript.



Code:


<?php
session_start();
if (!isset($_SESSION['endOfTimer'])){
    $endOfTimer = time() + 200;
    $_SESSION['endOfTimer'] = $endOfTimer;
}

$timeTilEnd = $_SESSION['endOfTimer'] - time();

if($timeTilEnd < 0) { 
session_destroy();
}
?>

You have <span id="timer"><?php echo $timeTilEnd; ?></span> seconds left


<script type="text/javascript">
var TimeLeft = <?php echo $timeTilEnd; ?>;

function countdown()
{
TimeLeft -= 1;
document.getElementById('timer').innerHTML = TimeLeft;
}
CountFunc = setInterval(countdown,1000);
</script>


----------



## jeffshead

Holly smokes... I've been "Googling" and playing around with this code all day!

Can someone help me please?

I need something like this, but I am a beginner and I don’t know what to change to suit my needs.

I have two pages that are “linked” to one another. That is to say they use hashes and time stamps so one can only access the second page if he/she was referred from the first page.

Page1 has the following redirect to Page2:


PHP:


<?php
	$link = "../folder/Page2.php";
    $t = time();
    $link = $link."?tstamp=".$t."&thash=".md5($t."sometext");
	header("refresh: 30; url=$link");
?>

Page2 has this PHP code at the top:


PHP:


<?php
$tstamp=$_REQUEST['tstamp'];
$thash=$_REQUEST['thash'];
$verifyhash=md5($tstamp."sometext");
if ($thash!=$verifyhash)
{
 header( 'refresh: 0; url=../folder/myErrorPage.php' );
 exit;
}
if ((time()-$tstamp)>600)
{
 header( 'refresh: 0; url=../folder/myErrorPage.php' );
 exit;
}
?>

1. Since I'm using a time stamp, can that be used instead of the session?
2. Because my countdown is ten minutes, I want the time remaining to be displayed in the following format: *10min 0sec*. When the time remaining gets down to less than 1 minute, I would want the displayed time to look this: *59sec*. Instead of this: *0min 59sec*.
3. When the time is up, redirect to another page.

Anyone willing to help me?


----------



## jeffshead

jeffshead said:


> Anyone willing to help me?


*Guess not.:sigh:*


----------



## jeffshead

Pyro-Fire said:


> fixed tajjyarden's code, and added javascript.
> 
> 
> 
> Code:
> 
> 
> <?php
> session_start();
> if (!isset($_SESSION['endOfTimer'])){
> $endOfTimer = time() + 200;
> $_SESSION['endOfTimer'] = $endOfTimer;
> }
> 
> $timeTilEnd = $_SESSION['endOfTimer'] - time();
> 
> if($timeTilEnd < 0) {
> session_destroy();
> }
> ?>
> 
> You have <span id="timer"><?php echo $timeTilEnd; ?></span> seconds left
> 
> 
> <script type="text/javascript">
> var TimeLeft = <?php echo $timeTilEnd; ?>;
> 
> function countdown()
> {
> TimeLeft -= 1;
> document.getElementById('timer').innerHTML = TimeLeft;
> }
> CountFunc = setInterval(countdown,1000);
> </script>


WOW! It's been quite some time since I've messed with this.

This script counts down into negative numbers. What code needs to be changed in order for the count down to be stopped at zero?

Also, what code needs to be changed to show the time in minutes and seconds then in seconds only when there is less than a minute left?


----------



## TheAtheist

jeffshead said:


> This script counts down into negative numbers. What code needs to be changed in order for the count down to be stopped at zero?


My best guess that



Code:


 if($timeTilEnd < 0) { 
session_destroy();

needs to read 



Code:


if($timeTilEnd =< 0) { 
session_destroy();

Might be an incorrect syntax, i'm sure someone will pipe up if i've got this wrong!!!

Regards
TheAtheist


----------

