PHP Countdown timer help

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
Found this cool PHP code on the interwebs which is ideal, it offers me a weekly countdown timer, but how would I adapt this to show hours left to buy a ticket on the last day?

At the moment it says tickets closed on the last day, but I want it to count down evey hour to a set time on the last day.

Can anyone help?

PHP:
<?php
 $interval = 3; // number of days between ticket sales
$launch_date = strtotime('22 January 2011'); // fill in
$days = floor((time() - $launch_date)/86400) % $interval;
if($days === 0)
{
    echo 'Ticket sales are now closed';
}
else
{
    echo '<p>Ticket sales close in '.((($days = $interval - $days)>($interval-1))?$days-$interval:$days).' days</p>';
} 

?>
 
If you're using PHP 5.3+ then have a look at the date_diff function, otherwise the below will get you the remaining hours (rounds down) -
Code:
$ticketdate=strtotime("25 January 2011");
$remainHours=round(($ticketdate-time())/3600);
 
Yeh I am using php 5.3+

How would I adapt it to print the Days Hours and min on the weekly countdown?

It has dawned on me that my timers will need to be set to 15:45 every Weds or say every friday at 20:15.

So it would read:

Ticket sales close in 4 days, 21 hours, 30 mins.

Then when the counter hits 0 days 0 hours 0 mins it would display "Ticket sales are closed" for a time period that I could change. After which it would then recommence it's weekly countdown to Weds at 15:45
 
Code:
$ticketdate = strtotime("25 January 2011");
$timediff = $ticketdate - time();

$remainDay = floor($timediff/(86400));
$remainHours = floor(($timediff-($remainDay*86400))/(3600));
$remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

Use an IF statement to check the remaining parts (note - negative numbers for past dates).

Edit - If you're using 5.3+ then you can use the date class (there are procedural functions rather than OO) which has a difference function (returns the difference between two dates/times as a unix timestamp, just format it accordingly).
 
Last edited:
I'm totally lost hehe

This is my first experience with a computer language :p

Would I not have to set
PHP:
$ticketdate = strtotime("25 January 2011");

to

PHP:
$ticketdate = strtotime("25 January 2011 21:00:00");

?
 
Here's what I use as a countdown timer:

PHP:
<?php
function getMytimeDiff($t1,$t2)
{
$a1 = explode(":",$t1);
$a2 = explode(":",$t2);
$time1 = (($a1[0]*60*60)+($a1[1]*60)+($a1[2]));
$time2 = (($a2[0]*60*60)+($a2[1]*60)+($a2[2]));
$diff = abs($time1-$time2);
$hours = floor($diff/(60*60));
$mins = floor(($diff-($hours*60*60))/(60));
$secs = floor(($diff-(($hours*60*60)+($mins*60))));
//$result = $hours.":".$mins.":".$secs;
$result = $hours.":".$mins;
return $result;
}


function getMins($res)
{
$array = explode(':',$res);
$hmins = ($array[0]*60);
$mins = $array[1];
$tmins = $hmins + $mins;
return $tmins;
}

date_default_timezone_set('Europe/London'); 

$mytime1 = "17:05:00";
$mytime2 = date("H:i:s");
$mytime3 = "12:30:00";

$cool = getMytimeDiff($mytime1,$mytime2); 
$homeTime = getMins($cool);

$homeTime2 = $homeTime/60;
$homeTime2 = round($homeTime2, 2);
$homeTime3 = $homeTime*60;
$homeTime3 = round($homeTime3, 2);

echo "Current Time :	$mytime2 <br>";
//echo "LT scheduled :	$mytime3 <br>";
echo "HT scheduled :	$mytime1 <p>";
echo "There are $homeTime minutes left until HT! <br>";
echo "There are $homeTime2 hours left until HT! <br>";
echo "There are $homeTime3 seconds left until HT! <br>";
?>
 
I'm totally lost hehe

This is my first experience with a computer language :p

Would I not have to set
PHP:
$ticketdate = strtotime("25 January 2011");

to

PHP:
$ticketdate = strtotime("25 January 2011 21:00:00");

?

You just need to check if the difference between the dates is smaller than or equal to zero, if so then ticket sale has finished -
Code:
if ($timediff < 0) {
    //Finished
} else {
    //Sale Still Running (show difference)
}


shine - You have to worry about location formats if you explode time/data strings, so you're better off working with timestamps (ie: strtotime function) and then format accordingly.
 
Last edited:
Ok can we go back to extreme basics? :D

PHP:
<?php 

$ticketdate = strtotime("30 January 2011");
$timediff = $ticketdate - time();

$remainDay = floor($timediff/(86400));
$remainHours = floor(($timediff-($remainDay*86400))/(3600));
$remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

print "Target date is: $ticketdate. Time Difference is: $timediff . Ticket sales close in $remainDay days, $remainHours hours, and $remainMins mins";

 ?>


In it's very basic form, it prints the right remaining days. How would I change $ticketdate for every Friday at 14:30?

So tickets close for sale every Friday at 14:30.
 
Sorry for my triple posting, didn't edit my posts so people who wish to help can see what a mess I am making :)

I now have a very basic working script.

PHP:
<?php 

$ticketdate =  mktime(19, 30, 0, 1, 25, 2011) ; //ticketdate is set to 25th Jan 2011 19:30
$timediff = $ticketdate - time();

$remainDay = floor($timediff/(86400));
$remainHours = floor(($timediff-($remainDay*86400))/(3600));
$remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

if ($timediff < 0) 

    echo "Sales Closed";

else 

    echo "Target date is: $ticketdate. Time Difference is: $timediff . Ticket sales close in $remainDay days, $remainHours hours, and $remainMins mins";

 ?>
Now before I try to go further with this how do I get this to repeat each week?

My event is weekly not a one time event.
 
Last edited:
PHP:
$ticketdate =  strtotime("friday 16:30", strtotime("now")) ;
$timediff = $ticketdate - time();

$remainDay = floor($timediff/(86400));
$remainHours = floor(($timediff-($remainDay*86400))/(3600));
$remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

if ($timediff < 0) 

    echo "Sales Closed";

else 

    echo "Target date is: $ticketdate. Time Difference is: $timediff . Ticket sales close in $remainDay days, $remainHours hours, and $remainMins mins";
 
shine - You have to worry about location formats if you explode time/data strings, so you're better off working with timestamps (ie: strtotime function) and then format accordingly.

You're right, I just put that together a while back when I was bored at work and was counting down until home time. :D
 
Last edited:
Back
Top Bottom