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

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 个不同的函数,它们 return 来自 unix 时间戳的人类可读字符串。 所有 3 个都无法工作

我希望有人看看这些函数并帮助我弄清楚如何将其中之一修复为 return 正确的人类可读字符串。

继续表演!

这是函数#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;
    }

我对这个函数的 return 值为 "Joined 1 days, 17 hours ago"

显然这是不正确的。

这是函数#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;
    }

我的此功能的 return 值是“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';
        }
    }
    }

这是我的代码和数据。 不太清楚为什么 none 似乎有效。我试着查看代码,但我不知道如何解决它。

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;

EXAMPLE

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

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

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

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

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

正如我在您的帖子中看到的那样,您总是将一年计算为 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