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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.