PHP Countdown timer help

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:
Well I’ve just tested this and it appears to work perfectly fine with the follow (only adjustment was to change the if condition to <= 0)-
Code:
$ticketdate =  strtotime("Wednesday 20:57", 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";
}

Do realise that the $timediff variable will become a negative number when the date/time passes. Either way, you shouldn't need to echo/print $ticketdate or $timediff as these two variables are just used for calculation.
 
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
 
At the moment it will reset when it gets to midnight/00:00. The below should do what you want though ie: continously countdown to the next date. Although i'm sure there is a better way of doing it.

Code:
$ticketdate = "Wednesday 21:30";
$timediff = (strtotime($ticketdate, strtotime("now"))) - time();
if ($timediff <=0) $timediff = (strtotime("Next ".$ticketdate, strtotime("now"))) - 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";
}
 
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 :)
 
The condition on line 3 checks to see if the current ticket sale day and time has passed, if so then $timediff is based on next weeks $ticketdate (hence the 'Next' in the strtotime() function).
If you want to delay it an hour then change that condition on line 3 to -
Code:
($timediff <= -3600)
(alternatively you could add an hour to your $ticketday time)
 
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?
 
Easiest would be turn it into a function (or class for OO), feed it the day/time (and possibly the delay between countdowns), and get it to return an array with the day, hour, minute integers for the countdown and a false boolean if ticket sales are over.
 
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!
 
Function -
Code:
// ** 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("Friday 16:55", 3600);

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

It's fairly straight forward, the ticketCountdown() function returns False if the ticket sale has ended otherwise it returns an Array with the remaining Days ($ticketSale1[0] etc), Hours ($ticketSale1[1] etc), Minutes ($ticketSale1[2] etc). You also have the option of adding a delay between countdowns (default is 0).
If you need multiple countdowns then simply duplicate the code between 'Countdown Routine' comments and change the 'echos' accordingly.
 
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
 
Code:
$ticketSale1 = ticketCountdown("Saturday 22:40", 30);
$ticketSale2 = ticketCountdown("Saturday 22:43", 30);

if ($ticketSale1) {
    echo "Ticket Sale 1 - $ticketSale1[0] Days, $ticketSale1[1] Hours, $ticketSale1[2] Minutes remain<br>";
    echo "Ticket Sale 2 - $ticketSale2[0] Days, $ticketSale2[1] Hours, $ticketSale2[2] Minutes remain<br>";
} else {
    echo "Ticket Sale - Ended<br>";
}
(You still need the ticketCountdown function from previous posts)

The IF condition checks to see if $ticketSale1 doesn't equal false, if true then it'll echo out ticket sales 1 and 2.
Also note that the delay is in seconds, not minutes. So a delay of 30 is 30 seconds! If you want that to be 30 minutes then change it to 1800 seconds.
 
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!
 
It probably would have been better and easier if you explained exactly what you were after in your first post as this has gone from a simple countdown to something a little more complex.

What is it you're actually after?
 
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:
PHP:
<?php 
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![/QUOTE]

You need to add more IF conditions between your current statements to check for returned 'false' (could also check if the return is an array, ie: is_array() func) from the ticketCountdown() function and then echo/print "0 Days 0 Hours 0 Mins" accordingly.
 
Back
Top Bottom