PHP倒数计时器问题? [英] PHP Countdown Timer Problem?

查看:35
本文介绍了PHP倒数计时器问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码编写了一个简单的PHP Countdown Timer:

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等平均目标时间,目标分钟...)

但是,此代码无法正确计算倒计时时间。如何解决这个问题?谢谢!

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

推荐答案

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

这篇关于PHP倒数计时器问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆