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>';
} 

?>
 
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
 
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");

?
 
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:
Still having issues with this, the time keeps going negative and not resetting for the next Weds at 6pm. any ideas?

PHP:
<?php 

$ticketdate =  strtotime("wednesday 18:00", 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 "Target date is: $ticketdate. Time Difference is: $timediff . Sales Closed";
    
else

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

 ?>
I suspect I have to tell the script to re-read $ticketdate when timediff = 0
 
Last edited:
It does trigger the sales closed echo but, it then doesn't reset itself and start counting down towards next wednesday at 21:10


Code:
Target date is: 1296076080. Time Difference is: -136 . Sales Closed
 
Thats perfect. Thank you so much!

On line 3, have you told it that if $timediff is equal to or less than 0 to fetch the "next" strtotime which creates a loop?


Out of interested how would I go about adding in a delay of say 60 mins between change over.

i.e.

if between Weds at 7:31 - 8:30

echo tickets closed

else continue countdown

Would I need to use the time function? How do you create an if statement for a time range?

PS: You have helped me enough, feel free to tell me to go away :)
 
Guys very good, I think I will have to use the +1 hour method tonight.

Out of interest, how would I go about counting down to two different days a week at different times?

Would I have to adapt the if statement from next to a new Ticketdate then loop it somehow?
 
Will look into, I think it's going to make many months of studying for me to fully understand how you built this script let alone to get it to do multiple days per week.

Hired a few books from the library to get me started.

Thank you all so much for your pacience!
 
PHP:
<?php 

// ** Countdown Function **
function ticketCountdown($ticketdate, $delay = 0) {
    $timediff = (strtotime($ticketdate, strtotime("now"))) - time();
    if ($timediff <= -$delay) $timediff = (strtotime("Next ".$ticketdate, strtotime("now"))) - time();

    if ($timediff <= 0) {
        return false; //Ticket Sale Ended
    } else {
        $remainDay = floor($timediff/(86400));
        $remainHours = floor(($timediff-($remainDay*86400))/(3600));
        $remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

        //array[0] = remaining Day(s)
        //array[1] = remaining Hour(s)
        //array[1] = remaining Minute(s)
        return array($remainDay, $remainHours, $remainMins);
    }
}
// ** Countdown Function **

//** Countdown Routine **
$ticketSale1 = ticketCountdown("Saturday 22:40", 30);

if ($ticketSale1) {
    echo "Ticket Sale 1 - $ticketSale1[0] Days, $ticketSale1[1] Hours, $ticketSale1[2] Minutes remain<br>";
} else {
    echo "Ticket Sale 1 - Ended<br>";
}
//** Countdown Routine **

//** Countdown Routine **
$ticketSale2 = ticketCountdown("Saturday 22:43", 30);

if ($ticketSale2) {
    echo "Ticket Sale 2 - $ticketSale2[0] Days, $ticketSale2[1] Hours, $ticketSale2[2] Minutes remain<br>";
} else {
    echo "Ticket Sale 2 - Ended<br>";
}
//** Countdown Routine **

 ?>

Thats brilliant! Only Issue I have is that it prints both sets of echo's for ticketsales1 and ticketsales2.

I assume I would have to tell it to only echo ticketsales2 if ticketsales1 has passed... going to need to research this, unless you still would like to help :p
 
Yeh I set it to 30 Seconds just to test it :p

What I am after is only echo'ing $tickeSales1 until it expires. When it does expire I would like the ticketSales1 echo to stop and to be replaced by targetSales2 echo
 
PHP:
<?php 

// ** Countdown Function **
function ticketCountdown($ticketdate, $delay = 0) {
    $timediff = (strtotime($ticketdate, strtotime("now"))) - time();
    if ($timediff <= -$delay) $timediff = (strtotime("Next ".$ticketdate, strtotime("now"))) - time();

    if ($timediff <= 0) {
        return false; //Ticket Sale Ended
    } else {
        $remainDay = floor($timediff/(86400));
        $remainHours = floor(($timediff-($remainDay*86400))/(3600));
        $remainMins = floor(($timediff-($remainDay*86400)-($remainHours*3600))/60);

        //array[0] = remaining Day(s)
        //array[1] = remaining Hour(s)
        //array[1] = remaining Minute(s)
        return array($remainDay, $remainHours, $remainMins);
    }
}
// ** Countdown Function **

//** Countdown Routine **
$ticketSale1 = ticketCountdown("Sunday 19:53", 30);
$ticketSale2 = ticketCountdown("Sunday 19:57", 30);

if ($ticketSale2 > $ticketSale1) {
    echo "Ticket Sale 1 - $ticketSale1[0] Days, $ticketSale1[1] Hours, $ticketSale1[2] Minutes remain<br>";
} if ($ticketSale2 < $ticketSale1) {
    echo "Ticket Sale 2 - $ticketSale2[0] Days, $ticketSale2[1] Hours, $ticketSale2[2] Minutes remain<br>";
}

 ?>

Semi fixed it all on my own!!

It now only displays one $ticketSales at a time, the only issue now is that it says Days Hours Min when in the delay peroid.

If only I could get it to read "0 Days 0 Hours 0 Mins" during the delay period for both taicketsales1 and ticketsales2 I would be in business!
 
Basically a countdown timer for several set days a week, which this does. However, it needs to only display one echo at a time.

so it would display the echo ticketsales1 then the $delay would kick in, changing the echo to "ticket sales closed" then when the $delay has passed for ticketsales1, it's echo would be removed and replaced with the echo's relating to ticketsales2.


Apologies for not being clear :(
 
Last edited:
Back
Top Bottom