无法从发现的各种函数中获取人类可读的文本


cannot get human readable text from various functions discovered

我正在尝试transform a unix timestamp into a human readable string so i can show how long ago a user signed up

这是我的数据:

    mysql> select createdate as unixtimestamp,date_format(from_unixtime(createdate),'%e %b %Y') as dateformatted from users where userid=40645;
    +---------------+---------------+
    | unixtimestamp | dateformatted |
    +---------------+---------------+
    |    1162642968 | 4 Nov 2006    |
    +---------------+---------------+
    1 row in set (0.00 sec)
    mysql>

好的,这就是问题所在。我在互联网上发现了3个不同的函数,它们从unix时间戳返回一个人类可读的字符串所有3个都无法工作

我希望有人看看这些函数,并帮助我找出如何修复其中一个函数,以返回正确的人类可读字符串

继续表演!

这里是功能#1:

    function getElapstedTimeHumanReadable($time)
    {
    $names = array("seconds", "minutes", "hours", "days", "months", "years");
    $values = array(1, 60, 3600, 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600);
    $time = time()-$time;
    for($i = count($values) - 1; $i > 0 && $time < $values[$i]; $i--);
    if($i == 0) {
        $timestamp = intval($time / $values[$i]) . " " . $names[$i];
    } else {
        $t1 = intval($time / $values[$i]);
        $t2 = intval(($time - $t1 * $values[$i]) / $values[$i - 1]);
        $timestamp= "$t1 " . $names[$i] . ", $t2 " . $names[$i - 1];
    }
    return $timestamp;
    }

此函数的返回值为"1天17小时前加入"

显然这是不对的

这里是功能#2:

    function getElapsedTimeHumanReadable($time)
    {
     $time = time() - $time;
     $points = array(
        'year'     => 31556926,
        'month'    => 2629743,
        'week'     => 604800,
        'day'      => 86400,
        'hour'     => 3600,
        'minute'   => 60,
        'second'   => 1
    );
    foreach($points as $point => $value)
    {
        if($elapsed = floor($time/$value) > 0)
        {
            $s = $elapsed>1?'s':'';
            $timestamp = "$elapsed $point$s";
            break;
        }
    }
    return $timestamp;
    }

此函数的返回值为"1天前加入

最后,这里是函数#3:

    function getElapsedTimeHumanReadable($time)
    {
    $etime=time()-$time;
    if ($etime < 1)
    {
        return '0 seconds';
    }
    $a = array( 365 * 24 * 60 * 60  =>  'year',
             30 * 24 * 60 * 60  =>  'month',
                  24 * 60 * 60  =>  'day',
                       60 * 60  =>  'hour',
                            60  =>  'minute',
                             1  =>  'second'
            );
    $a_plural = array( 'year'   => 'years',
                   'month'  => 'months',
                   'day'    => 'days',
                   'hour'   => 'hours',
                   'minute' => 'minutes',
                   'second' => 'seconds'
            );
    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
    }

这就是我的代码和数据不太清楚为什么一个都不起作用。我试着查看代码,但不知道如何解决。

Whats interesting is they all say 2 days, but my timestamp appears to show 2006.

谢谢你的帮助。

$time = 1162642968 ;
$date = new DateTime( );
$date->setTimestamp( $time );
$today = new DateTime( 'now', new DateTimeZone( "Europe/Rome" ) );
$diff = $today->diff( $date);
echo "Year: " . $diff->y . " - Month: " . $diff->m . " - Days: " . $diff->d . " - Hours: " . $diff->h;

示例

按照建议,我会添加解释,即使我认为这真的是自我解释。

$date=new DateTime()创建对象,$date->setTimestamp($time)用于将该日期设置为mysql时间戳中的值。

$today是在实际日期创建的。

$date->diff()创建一个DateInterval对象(http://php.net/manual/en/class.dateinterval.php),其中包含所有必要的数据。

如果你想自己解决这个问题,你应该计算差值并以这些值为基础。我还没有测试RiccardoC的Answer,但这似乎是一个不错的方法。

正如我在你的帖子中所看到的,你总是将一年计算为365天,所以如果你不想深入了解时区、额外的小时、额外的天数、不同的月份长度等细节,你可以使用这样简单的方法:

function getElapsedTimeHumanReadable($timestamp) { 
    $diff = time() - $timestamp;
    $years = intval($diff/31536000); //seconds in a year 60*60*24*365
    $diff -= ($years*31536000);
    $months = intval($diff/2592000); //seconds in a month 60*60*24*30
    $diff -= ($months*2592000);
    $days = intval($diff/86400); //seconds in a day 60*60*24
    return $years." years, ".$months." months, ".$days." days ago";
}
echo getElapsedTimeHumanReadable(1162642968); // November 4th, 2006

回声9 years, 0 months, 17 days ago