2011-05-17 96 views
1

我已經使用這個代碼編碼一個簡單的PHP倒數計時器:PHP倒數計時器問題?

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等平均目標時,目標分鐘...)
但是,此代碼不計算倒計時時間適當。如何解決這個問題?謝謝!

+0

你有沒有自己檢查過,或者你只是在有人會做你的工作之前等待? – zerkms 2011-05-17 03:00:43

+1

正確設置代碼格式(縮進)。 – dkamins 2011-05-17 03:02:34

回答

1
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'); 
}