相对日期格式总是输出“;3小时前”;


Relative date formatting always outputs "3 hours ago"

我正在使用php计算指定日期和当前日期之间的日期间隔。我这样做是为了打印出社交友好的时间戳,如A few mins ago2 hours ago

当我谈到小时部分时,我在php中得到了一些行为非常有趣的东西。以下是完整的工作代码,但当您用此代码替换hours部分时,它总是打印出3个小时。

定义常数DATE

// The current date timestamp
define('DATE', time());

Buggy代码:

//Only the hours part that' doing something weird
case ($interval >= 3600 && $interval < 86400) :
            $return = ( date('H', $interval) < 2)
                ? (int)date('H', $interval) . ' hour ago'
                : (int)date('H', $interval) . ' hours ago';
            break;

当指定的日期,比如说在本例中,创建日期刚好在一个多小时前时,它就会中断,从而导致间隔等于3660秒。似乎日期方法调用date('H', 3660)的结果是03。这不应该导致01吗?毕竟才一个多小时。

此处的工作代码:

public static function getTimeInterval($date)
{
    $interval = DATE - $date;
    $return = '';
    switch ( $interval )
    {
        case ($interval <= 60) :
            $return = 'a few secs ago';
            break;
        case ($interval > 60 && $interval < 3600) :
            $return = (int)date('i', $interval) . ' mins ago';
            break;
        case ($interval >= 3600 && $interval < 86400) :
            $return = ( abs((date('G', DATE) - date('G', $date))) < 2)
                ? abs((date('G', DATE) - date('G', $date))) . ' hour ago'
                : abs((date('G', DATE) - date('G', $date))) . ' hours ago';
            break;
        case ($interval >= 86400 && $interval < 604800) :
            $return = ( (int)date('j', $interval) === 1)
                ? (int)date('j', $interval) . ' day ago'
                : (int)date('j', $interval) . ' days ago';
            break;
        case ($interval > 604800 && $interval <= 2592000) :
            $return = 'A few weeks ago';
            break;
        case ($interval > 2592000) :
            $return = date('n', $interval) . ' months ago';
            break;
        case ($interval > 31536000) :
            $return = 'Over a year ago';
            break;
    }
    return $return;
}
日期()的结果取决于您的时区。您可以通过date_default_timezone_set()手动设置时区来更改此行为

检查一下这个代码,它运行良好:

function do_plurals($nb, $str)
{
    return $nb > 1 ? $str . 's' : $str;
}

function dates_interval_text(DateTime $start, DateTime $end)
{
    $interval = $end->diff($start);
    $format = array();
    if ($interval->y !== 0)
    {
        $format[] = "%y " . do_plurals($interval->y, "year");
    }
    if ($interval->m !== 0)
    {
        $format[] = "%m " . do_plurals($interval->m, "month");
    }
    if ($interval->d !== 0)
    {
        $format[] = "%d " . do_plurals($interval->d, "day");
    }
    if ($interval->h !== 0)
    {
        $format[] = "%h " . do_plurals($interval->h, "hour");
    }
    if ($interval->i !== 0)
    {
        $format[] = "%i " . do_plurals($interval->i, "minute");
    }
    if (!count($format))
    {
        return "less than a minute ago";
    }
    // We use the two biggest parts
    if (count($format) > 1)
    {
        $format = array_shift($format) . " and " . array_shift($format);
    }
    else
    {
        $format = array_pop($format);
    }
    // Prepend 'since ' or whatever you like
    return $interval->format($format) . ' ago';
}

它会产生类似2 hours and 3 minutes ago 的结果

date('H', 3660)对我来说是01。也许是您所在的时区正在应用。这可以是在本地级别(应用程序)或全局级别(php-config)。甚至可能是你的操作系统。很抱歉,我无法提供更多的帮助来查找哪些PHP设置等等。

date的第二个参数不是持续时间,而是时间戳date回答了"1970年1月1日00:00 UTC后N秒,现在是什么时间?"问题的答案是在您的时区中给出的,因此您的时区偏移量会添加到答案中。

使用date('Z')查找您的时区偏移量(以秒为单位)。或者,更好的是,使用floor($time/3600)来计算小时数。

您的代码运行良好。您调用它的方式有错误。当$date作为unix时间戳传递时,它运行得非常好例如$date=time()-1*60*60(1小时前)。

http://codepad.org/HS25qThK

$aboutHourAgo = time() - 2.5*60*60;   // 2 and half hours ago
var_dump(getTimeInterval($aboutHourAgo));

我的第一个问题是,您从哪里获得时间/时间戳/日期时间?

在我们公司,我们在德国运行了一些服务器,这些服务器上的应用程序适用于巴基斯坦(GMT+5)。我们使用这个函数,它使用Unix时间戳来返回"人类可读"的日期时间:-

/**
 * @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
 * @param number $timestamp
 * @return string
 */
function readable_time($timestamp, $num_times = 2)  {
    // this returns human readable time when it was uploaded
    $times = array (
    31536000 => 'year', 2592000 => 'month',
    604800 => 'week', 86400 => 'day',
    3600 => 'hour', 60 => 'minute', 1 => 'second'
    );
    $now = time ();
    $secs = $now - $timestamp;
    // Fix so that something is always displayed
    if ($secs == 0) $secs = 1;
    $count = 0; $time = '';
    foreach ( $times as $key => $value )    {
        if ($secs >= $key)  {
            // time found
            $s = '';
            $time .= floor ( $secs / $key );
            if ((floor ( $secs / $key ) != 1))  $s = 's';
            $time .= ' ' . $value . $s;
            $count ++;
            $secs = $secs % $key;
            if ($count > $num_times - 1 || $secs == 0) break;
            else $time .= ', ';
        }
    }
    return ($time != "") ? $time." ago" : "now";
}

通过修改$num_times变量,我们可以得到类似于"4个月、3天、2小时、3分钟、15秒前"的内容(在本例中,将变量设置为5)。

从逻辑上讲,时间戳来自数据库——在我们的例子中是MySQL——所以我们使用另一个函数,它获取MySQL时间戳并将其转换为Unix时间戳:-

/**
 * @desc Converts UNIX Time into human readable time like '1 hour', 3 weeks', etc.
 * @param number $timestamp
 * @return string
 */
function RelativeTime($timestamp)   {
    $difference = time() - $timestamp;
    $periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
    $lengths = array("60", "60", "24", "7", "4.35", "12", "10");
    if ($difference > 0)    { // this was in the past
        $ending = "ago";
    }   else    { // this was in the future
        $difference = -$difference;
        $ending = "to go";
    }
    for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
    $difference = round($difference);
    if($difference != 1) $periods[$j].= "s";
    $text = "$difference $periods[$j] $ending";
    return $text;
}

祝你好运!