t.bo Posted August 26, 2007 Share Posted August 26, 2007 Hello everybody, I'm making a countdown timer that displays the days left to a certain event. When its the day of the event it should display 0 days left. The problem with my script is that it returns 1 day too short. I could just add "+ 1" to it but then it would never be 0 days left on the day of the event. <?php function CountDown($date) { // Timestamp the inputed date $countdown_date = strtotime($date); echo $date ."<br />"; // Get today's date $today = time(); $difference = $countdown_date - $today; if($difference <0) $difference = 0; $days_left = floor($difference/60/60/24); echo "Countdown date ".date("F j, Y, g:i a",$countdown_date)."<br/>"; if($days_left == 0) { return "Nog {$days_left} dagen te gaan!!!"; } } $date = "2007-09-08"; $result = CountDown($date); echo "{$result}"; ?> Hope someone helps me out. Thanks in advance grtz t.bo Quote Link to comment https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/ Share on other sites More sharing options...
Barand Posted August 26, 2007 Share Posted August 26, 2007 use $today = mktime(0,0,0); to eliminate the time element from the date Quote Link to comment https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/#findComment-334690 Share on other sites More sharing options...
t.bo Posted August 26, 2007 Author Share Posted August 26, 2007 If I use $today = mktime(0,0,0); instead of $today = time() then the result is empty... ? Quote Link to comment https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/#findComment-334696 Share on other sites More sharing options...
Barand Posted August 26, 2007 Share Posted August 26, 2007 with time() <?php $countdown_date = strtotime('2007-09-08'); $today = time(); $difference = $countdown_date - $today; $days_left = floor($difference/86400); echo $days_left; // 11 ?> with mktime(0,0,0) <?php $countdown_date = strtotime('2007-09-08'); $today = mktime(0,0,0); $difference = $countdown_date - $today; $days_left = floor($difference/86400); echo $days_left; // 12 ?> The time portion of $today is corrupting your results <?php $t = time(); echo date('Y-m-d H:i:s', $t); // 2007-08-27 00:21:36 $t = mktime(0,0,0); echo date('Y-m-d H:i:s', $t); // 2007-08-27 00:00:00 ?> Quote Link to comment https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/#findComment-334795 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 fyi to make this a lot cooler look into javascript so you can have it realistically "Count down" using a javascript clock idea. You can find javascript countdown timmers all over. Quote Link to comment https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/#findComment-334798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.