Php 上次看到的Unix时间戳

Php 上次看到的Unix时间戳,php,converter,unix-timestamp,Php,Converter,Unix Timestamp,你能帮帮我吗 我有一个unix时间戳,我想将其转换为最后看到的函数 例如: 当前unix时间戳-上次看到的时间戳=2天19小时05分钟81秒 function ago($mtime) { $xtime = time() - $mtime; if ($xtime < 1) { return '0 seconds'; } $a = array( 12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60

你能帮帮我吗

我有一个unix时间戳,我想将其转换为最后看到的函数

例如:

当前unix时间戳-上次看到的时间戳=2天19小时05分钟81秒

function ago($mtime)
{
$xtime = time() - $mtime;

if ($xtime < 1)
{
    return '0 seconds';
}

$a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
            30 * 24 * 60 * 60       =>  'month',
            24 * 60 * 60            =>  'day',
            60 * 60                 =>  'hour',
            60                      =>  'minute',
            1                       =>  'second'
            );

foreach ($a as $secs => $str)
{
    $d = $xtime / $secs;
    if ($d >= 1)
    {
        $r = round($d);
        return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
    }
}}
函数日期($mtime)
{
$xtime=时间()-$mtime;
如果($xtime<1)
{
返回“0秒”;
}
$a=数组(12*30*24*60*60=>“年”,
30*24*60*60=>“月”,
24*60*60=>“天”,
60*60=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($a作为$secs=>$str)
{
$d=$xtime/$secs;
如果($d>=1)
{
$r=四舍五入($d);
返回$r.'.$str。($r>1?'s':'')。'ago';
}
}}

您好,我找到了一个更快、更易于使用的解决方案。谢谢你的答复

function ago($unix)
{
    $datetime1 = new DateTime(date("Y-m-d H:i:s", $unix));
    $datetime2 = new DateTime();
    $interval = $datetime1->diff($datetime2);
    return $interval->format('%a days, %H hours, %I min and %S sec  ');
}

它返回0天、19小时、33分钟和31秒

Unix时间戳表示为自1970年1月1日以来经过的秒数。因此,您的结果将是两个时间戳之间经过的秒数。您只需将结果转换为分钟/小时/天…好的,我这样做了,它只显示我想要的小时数天小时分钟和秒您可以通过在
format()
方法中使用
%a
避免将
$months
乘以30(某些月份有31天)。