2

I have coded a simple PHP Countdown Timer using this code:

list($date,$time)=explode(" ",$compounddate);
list($thour,$tminute,$tsecond)=explode(":",$time);
list($tyear,$tmonth,$tday)=explode(":",$date);
$date=str_replace(":","/",$date);
$target = mktime($thour,$tminute,$tsecond,$tmonth,$tday,$tyear) ;

$today = time () ;

$difference =($target-$today) ;

$days = floor(abs($difference/86400));

$hours = floor(abs($difference%86400/3600));

$minutes =floor(abs($difference%3600/60));

$seconds = floor(abs($difference%60));

$date = date("F d, Y ",strtotime($date));
$time = date("g:i:s A",strtotime($time));
$past_future="";
$boolpast_future=true;
if($difference<0) {
$past_future="<strong> Time since ".$date." at ".$time.": </strong>";
$boolpast_future=false;
}
else {
$past_future="<strong> Countdown to ".$date." at ".$time.": </strong>";
$boolpast_future=true;
}
}

($thour, $tminute, etc mean target hour, target minute...)
However, this code does not calculate the countdown time properly. How to fix this? Thanks!

2
  • Have you examined it yourself or you're just waiting before someone will do your work? Commented May 17, 2011 at 3:00
  • 1
    Format your code properly (indents). Commented May 17, 2011 at 3:02

1 Answer 1

2
date_default_timezone_set('Asia/Tokyo');

$target = new DateTime('2011-06-01 05:00:00');
$now = new DateTime;
$diff = $target->diff($now);

if ($target > $now) {
    echo $diff->format('%a days, %h hours, %i minutes to go');
} else {
    echo $diff->format('%a days, %h hours, %i minutes too late');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.