echo date('H:i', time()); // 10:15
echo date('H:i', strtotime($this->deadline)); // 10:05
$delay = time() - strtotime($this->deadline);
echo date('H:i', $delay); // 02:10
为什么delay
是2小时10分钟而不是10分钟?
我认为它与时区有关。现在是欧洲/赫尔辛基。但是,如何在两个时间戳之间获得绝对差异呢?
修改
echo time(); // 1339745334
echo strtotime($this->deadline); // 1339657500
答案 0 :(得分:1)
$ delay实际上并不是一个合适的时间戳。这只是两个时间戳之间的差异,可能低至1. mktime()函数在这里很有用。
答案 1 :(得分:0)
试试此代码
date_diff($time_start, $time_ends);
function date_diff($d1, $d2){
$d1 = (is_string($d1) ? strtotime($d1) : $d1);
$d2 = (is_string($d2) ? strtotime($d2) : $d2);
$diff_secs = abs($d1 - $d2);
$base_year = min(date("Y", $d1), date("Y", $d2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array(
"years" => date("Y", $diff) - $base_year,
"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
"months" => date("n", $diff) - 1,
"days_total" => floor($diff_secs / (3600 * 24)),
"days" => date("j", $diff) - 1,
"hours_total" => floor($diff_secs / 3600),
"hours" => date("G", $diff),
"minutes_total" => floor($diff_secs / 60),
"minutes" => (int) date("i", $diff),
"seconds_total" => $diff_secs,
"seconds" => (int) date("s", $diff)
);
}
答案 2 :(得分:0)
如果要显示实时,则需要根据差异格式化时间。这是一个示例函数,您可以根据需要进行扩展:
function time_diff($format,$seperator,$delay){
$days = floor($delay/86400);
$hours = floor(($delay%86400)/3600);
$mins = floor(($delay%3600)/60);
$secs = floor(($delay%60));
$format = explode($seperator,$format);
$return = "";
foreach($format as $value){
if(strlen($return) > 0){
$return .= $seperator;
}
switch($value){
case 'H':{
$return .= $hours;
break;
}
case 'i':{
$return .= $mins;
break;
}
case 'z':{
$return .= $days;
break;
}
case 's':{
$return .= $secs;
break;
}
}
return $return;
}
用法:function_time_diff("H:i",':',$delay)
=>你的延迟格式化了。