Jump to content

Countdown timer (days to a date)


t.bo

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/66793-countdown-timer-days-to-a-date/
Share on other sites

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.