//Example data
$current_time = 1318075950;
$unbanned_time = $current_time + strtotime('+1 minute');
if ($unbanned_time > $current_time) {
$th1is = date('Y-m-d H:i:s', $unbanned_time) - date('Y-m-d H:i:s', $current_time);
echo date('Y-m-d H:i:s', $th1is);
我正试图输出它是多长时间,直到用户被禁止…年、月、日、时、分、秒……但是这给了我一些奇怪的结果…
您应该查看如何使用日期/时间函数的手册。
首先,代替
$current_time + strtotime('+1 minute')
使用strtotime('+1 minute', $current_time);
(参见strtotime
手册)。
其次,date
函数返回字符串。在大多数情况下,减去两个字符串并不是很有用。
if ($unbanned_time > $current_time) {
$th1is = $unbanned_time - $current_time;
echo $th1is/3600 . ' hours';
}
这将输出以小时为单位的剩余时间,但是有许多可用的函数可以产生更好的格式(或者您可以自己编写一个)。
我建议使用DateTime
$DateTime = new DateTime();
$unbanned_DateTime = new DateTime();
$unbanned_DateTime = $unbanned_DateTime->modify('+1 minute');
if ( $unbanned_DateTime > $DateTime ) {
$interval = $DateTime->diff($unbanned_DateTime);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%s');
}
您可以对一个输出使用->format(),而不是使用每个单独的值作为变量。随你便。
记住DateTime->format()需要在php.ini或
中设置一个时区date_default_timezone_set('....');
date()
返回一个字符串,这里减去两个字符串没有意义。您可以使用基本的数学计算剩余时间:
<?php
$current_time = time();
$unbanned_time = /* whatever */;
$seconds_diff = $unbanned_time - $current_time();
echo "You're unbanned at " . date("Y-m-d H:i:s", $unbanned_time) . " which is over ";
if ($seconds_diff <= 120) {
echo "$seconds_diff seconds";
} else if ($seconds_diff <= 7200) {
echo floor($seconds_diff / 60) . " minutes";
} else if ($seconds_diff <= 7200 * 24) {
echo floor($seconds_diff / 3600) . " hours";
} else {
echo floor($seconds_diff / 3600 / 24) . " days";
}
?>