0

$lastLoginTime = date("Ymd H:i:s"); //最后一次登录,登录时将存储在数据库中

假设上次登录是:2012-05-11 07:57:46
当前时间是:$current_time = date("Ymd H:i:s")

    $diff_seconds  = strtotime($current_time) - strtotime($lastLoginTime);
    $diff_weeks    = floor($diff_seconds/604800);
    $diff_seconds -= $diff_weeks   * 604800;
    $diff_days     = floor($diff_seconds/86400);
    $diff_seconds -= $diff_days    * 86400;
    $diff_hours    = floor($diff_seconds/3600);
    $diff_seconds -= $diff_hours   * 3600;
    $diff_minutes  = floor($diff_seconds/60);
    $diff_seconds -= $diff_minutes * 60;



    echo "You last logged in $diff_weeks weeks, $diff_days days, ";
    echo "$diff_hours hours, $diff_minutes minutes, and $diff_seconds minutes ago.";

错误的输出......!:-(

4

1 回答 1

1

谷歌(试一试)给了我这个:

<?php
function duration($seconds, $max_periods) {
    $periods = array("year" => 31536000, "month" => 2419200, "week" => 604800, "day" => 86400, "hour" => 3600, "minute" => 60, "second" => 1);
    $i = 1;
    foreach ( $periods as $period => $period_seconds )
    {
        $period_duration = floor($seconds / $period_seconds);
        $seconds = $seconds % $period_seconds;
        if ( $period_duration == 0 ) continue;
        $duration[] = "{$period_duration} {$period}" . ($period_duration > 1 ? 's' : '');
        $i++;
        if ( $i >  $max_periods ) break;
    }
    return implode(' ', $duration);
}
$diff_seconds  = strtotime($current_time) - strtotime($lastLoginTime);

print(duration($diff_seconds, 6));
?>

http://www.hawkee.com/snippet/2056/

于 2012-05-11T08:33:14.033 回答