0
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 

为什么delay2 小时 10 分钟而不是 10 分钟?

我认为它与时区有关。现在是欧洲/赫尔辛基。但是我怎样才能得到两个时间戳之间的绝对差异呢?

编辑

echo time(); // 1339745334
echo strtotime($this->deadline); // 1339657500
4

3 回答 3

1

$delay 实际上不是一个正确的时间戳。这只是两个时间戳之间的差异,可能低至 1。 mktime() 函数在这里可能很有用。

于 2012-06-15T07:25:15.347 回答
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)=>您的延迟已格式化。

于 2012-06-15T08:16:07.717 回答
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)
        );
    }
于 2012-06-15T07:32:18.127 回答