PHP 倒计时至今
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Countdown to Date
提问by usertest
How could set a date and get a countdown in PHP? For example if I set the date as 3 December 2PM it would tell me how many days and hours are remaining.
如何在 PHP 中设置日期并获得倒计时?例如,如果我将日期设置为 12 月 3 日下午 2 点,它会告诉我剩余的天数和小时数。
No need for user inputs for the date as it will be hard coded.
不需要用户输入日期,因为它将被硬编码。
Thanks.
谢谢。
回答by Travis
You can use the strtotime function to get the time of the date specified, then use time to get the difference.
可以使用 strtotime 函数获取指定日期的时间,然后使用 time 获取差异。
$date = strtotime("December 3, 2009 2:00 PM");
$remaining = $date - time();
$remaining will be the number of seconds remaining. Then you can divide that number to get the number of days, hours, minutes, etc.
$remaining 将是剩余的秒数。然后,您可以将该数字除以得到天数、小时数、分钟数等。
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left";
回答by Izhar Aazmi
Let me play around like this:
让我这样玩:
$rem = strtotime('2012-08-01 14:00:00') - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";
Try this at your leisure... :-)
闲暇时试试这个... :-)
NOTE: There is no if()test for echo "Remaining...", just coz you wont process this in case when $rem <= 0. Isn't it?
注意:没有if()测试echo "Remaining...",只是因为您不会在 $rem <= 0. 不是吗?
回答by GZipp
PHP 5.3 allows this:
PHP 5.3 允许这样做:
$dt_end = new DateTime('December 3, 2009 2:00 PM');
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';
回答by Kaspars Foigts
It's not as trivial as subtracting strtotime() results, since there are daylight savings and time would be mathematically correct, but not physically. Anyway, for these purposes you should use gmdate() function, which has no daylight savings:
它不像减去 strtotime() 结果那么简单,因为有夏令时,时间在数学上是正确的,但在物理上不是。无论如何,出于这些目的,您应该使用没有夏令时的 gmdate() 函数:
$date = gmdate('U', strtotime('2009-12-03 14:00'));
// Get difference between both dates without DST
$diff = $date - gmdate('U');
// Days (in last day it will be zero)
$diff_days = floor($remaining / (24 * 60 * 60));
// Hours (in the last hour will be zero)
$diff_hours = floor($remaining % (24 * 60 * 60) / 3600);
回答by JonnySchieds
Did this countdown until the end of the semester:
这个倒计时直到学期结束:
$endOfSemester = mktime(15,30,0,5,21,2015);
$now = time();
$secondsRemaining = $endOfSemester - $now;
define('SECONDS_PER_MINUTE', 60);
define('SECONDS_PER_HOUR', 3600);
define('SECONDS_PER_DAY', 86400);
$daysRemaining = floor($secondsRemaining / SECONDS_PER_DAY); //days until end
$secondsRemaining -= ($daysRemaining * SECONDS_PER_DAY); //update variable
$hoursRemaining = floor($secondsRemaining / SECONDS_PER_HOUR); //hours until end
$secondsRemaining -= ($hoursRemaining * SECONDS_PER_HOUR); //update variable
$minutesRemaining = floor($secondsRemaining / SECONDS_PER_MINUTE); //minutes until end
$secondsRemaining -= ($minutesRemaining * SECONDS_PER_MINUTE); //update variable
echo("<h3>There are $daysRemaining days, $hoursRemaining hours, $minutesRemaining minutes, $secondsRemaining seconds until the end of the semester</h3>"); //print message
回答by huzi8t9
Using @Izhar Aazmi solution, you could set this up nicely for display, as such:
使用@Izhar Aazmi 解决方案,您可以很好地设置它以进行显示,如下所示:
public function countdown($time, $h = true, $m = true, $s = true) {
$rem = $time - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if ( $day && !$h ) {
if ( $hr > 12 ) $day++; // round up if not displaying hours
}
$ret = Array();
if ( $day && $h ) $ret[] = ($day ? $day ." day".($day==1?"":"s") : "");
if ( $day && !$h ) $ret[] = ($day ? $day . " day" . ($day == 1 ? "" : "s") : "");
if ( $hr && $h ) $ret[] = ($hr ? $hr ." hour" . ($hr==1?"":"s") : "");
if ( $min && $m && $h ) $ret[] = ($min ? $min ." minute". ($min==1?"":"s") : "");
if ( $sec && $s && $m && $h ) $ret[] = ($sec ? $sec ." second".($sec==1?"":"s") : "");
$last = end($ret);
array_pop($ret);
$string = join(", ", $ret)." and {$last}";
return $string;
}
I hope this helps! It's a nice clean way or displaying the countdown.
我希望这有帮助!这是一个很好的干净的方式或显示倒计时。
回答by WiMantis
For those looking for a function capable of handling larger and smaller time span (php >5.3) :
对于那些正在寻找能够处理越来越小的时间跨度(php > 5.3)的功能的人:
/**
* Return a textual representation of the time left until specified date
*/
function timeleft(DateTime $date){
$now = new DateTime();
if($now > $date){
return '0 second';
}
$interval = $date->diff($now);
if($interval->y){
return $interval->format("%y year").($interval->y > 1 ? 's':'');
} else if($interval->m){
return $interval->format("%m month").($interval->m > 1 ? 's':'');
} else if($interval->d){
return $interval->format("%d day").($interval->d > 1 ? 's':'');
} else if($interval->h){
return $interval->format("%h hour").($interval->h > 1 ? 's':'');
} else if($interval->i){
return $interval->format("%i minute").($interval->i > 1 ? 's':'');
} else if($interval->s) {
return $interval->format("%s second").($interval->s > 1 ? 's':'');
} else {
return 'milliseconds';
}
}

