0

我正在一个网站上使用此代码作为日期

if($fetch[type] == '2'){echo 'photo to ';}
        echo ' tutor profile</td>';
        echo '<td valign="top"><span>';
        echo $fetch['time'];
        echo '</tr></table></li>';

并得到结果: 在此处输入图像描述

但我希望它应该显示结果为:1 年前或 1 年 2 个月前或 2 天前或 11 小时 30 分钟前......就是这样。

4

4 回答 4

3

这将计算从您的日期过去了多少天

$date = strtotime("2012-01-04");
$now = time();   
$time = $now - $date;
$newdate = floor($time/(60*60*24));

echo $newdate . ' days ago.';

这将输出

491 days ago.
于 2013-05-09T06:10:20.720 回答
1

以最小的单位获取时差(为了论证,假设为秒)。

然后得到一个限制,告诉你什么时候从一个近似值过渡到下一个近似值。

例如

<?php

    $appro = array(
        array( 1, 'second', 60 ), // Up to 60 seconds it's seconds
        array(60, 'minute', 60 ), // Divide by 60, get minutes. Up to 60 minutes is OK
        array(60, 'hour', 24),    // Divide by 60, get hours. Up to 24 is OK
        array(24, 'day', 7),      // Divide by 24, get days.
        array(7, 'week', 5),      // Divide by 7 to get weeks
        array(30.0/7.0, 'month', 12), // Re-multiply by 7 and divide by 30: months
        array(365.0/30.0, 'year', 99), // Get days again, divide by 365
    );

    $timediff = 366*24*3600; // Get this timediff somehow, in seconds

    $ap = $timediff;
    foreach($appro as $check)
    {
        list($scale, $name, $maximum) = $check;
        $ap /= $scale;
        if ($ap < $maximum)
        {
            print "Scale=$scale, $ap\n";
            $what = $name;
            switch (floor($ap))
            {
                case 0: $what = "less than one $name"; break;
                case 1: $what = "one $name"; break;
                default: $what = floor($ap) . " {$name}s"; break;
            }
            break;
        }
    }
    print "This was $what ago.";
?>

当然,这会说1.5年前是“一年前”。可以修改算法,让余数乘以$scale下面的单位渲染,这样1.5年就变成了“一年零六个月”。这有时也让人有点尴尬,因为 9 天变成了“一周零两天前”。

如果近似正确,可以扩展算法并尝试在最合适的单位(上面的一个和下面的一个)中渲染一个周期,最后选择最短的形式(所以“九天”胜过“一周和两天”,“六周”胜过“一个月零两周”)。

于 2013-05-09T06:37:59.407 回答
1

尝试这个:

<?php
          function time_since($since) 
          {
                  $chunks = array(
                          array(60 * 60 * 24 * 365 , 'year'),
                          array(60 * 60 * 24 * 30 , 'month'),
                          array(60 * 60 * 24 * 7, 'week'),
                          array(60 * 60 * 24 , 'day'),
                          array(60 * 60 , 'hour'),
                          array(60 , 'minute'),
                          array(1 , 'second')
                  );

                  for ($i = 0, $j = count($chunks); $i < $j; $i++) {
                          $seconds = $chunks[$i][0];
                          $name = $chunks[$i][1];
                          if (($count = floor($since / $seconds)) != 0) {
                                  break;
                          }
                  }

                  $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
                  return $print;
          }
           $DT = strtotime("2012-06-10");
           echo time_since(time()-$DT);

          ?>

IDEONE 演示

演示 2

演示 3

于 2013-05-09T06:22:28.107 回答
1

像这样的代码:

<?php

date_default_timezone_set("Europe/Tallinn");

$dates = Array(
    '2012-11-30',
    '2012-06-27',
    '2012-03-04',
    '2012-01-05',
    '2012-01-04'
);



foreach ($dates as $d)
{
    $unix_timestamp = strtotime($d);
    //As no time set it shows time as 00:00:00, as a day beginning time
    echo date("d.m.Y H:i:s", $unix_timestamp) . "<br>";
    $current_unix_timestamp = time();
    //Assuming all dates as in past
    $diff = $current_unix_timestamp - $unix_timestamp;
    echo ago($diff);
    echo '<br><br>';
}

function ago($diff)
{
    $second = 1;
    $minute = $second * 60;
    $hour   = $minute * 60;
    $day    = $hour * 24;
    $month  = $day * 27.554551; //average, will not give you exact difference, but close enough
    $year   = $day * 365;

    $ret = "";
    if (floor($diff/$year)!=0)
    {
        $ret .= floor($diff/$year) . ' year' . (floor($diff/$year)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$year) * $year;
    }

    if (floor($diff/$month)!=0)
    {
        $ret .= floor($diff/$month) . ' month' . (floor($diff/$month)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$month) * $month;
    }

    if (floor($diff/$day)!=0)
    {
        $ret .= floor($diff/$day) . ' day' . (floor($diff/$day)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$day) * $day;
    }

    if (floor($diff/$hour)!=0)
    {
        $ret .= floor($diff/$hour) . ' hour' . (floor($diff/$hour)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$hour) * $hour;
    }

    if (floor($diff/$minute)!=0)
    {
        $ret .= floor($diff/$minute) . ' minute' . (floor($diff/$minute)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$minute) * $minute;
    }

    if (floor($diff/$second)!=0)
    {
        $ret .= floor($diff/$second) . ' seconds' . (floor($diff/$second)>1?'(s)':'') . ' ';
        $diff  -= floor($diff/$second) * $second;
    }

    $ret .= 'ago';

    return $ret;
}
?>

将在 2013-05-09 09:29 给出这样的结果

30.11.2012 00:00:00
5 month(s) 22 day(s) 13 hour(s) 55 minute(s) 22 seconds(s) ago

27.06.2012 00:00:00
11 month(s) 13 day(s) 7 hour(s) 4 minute(s) 3 seconds(s) ago

04.03.2012 00:00:00
1 year 2 month(s) 11 day(s) 5 hour(s) 51 minute(s) 2 seconds(s) ago

05.01.2012 00:00:00
1 year 4 month(s) 15 day(s) 3 hour(s) 13 minute(s) 56 seconds(s) ago

04.01.2012 00:00:00
1 year 4 month(s) 16 day(s) 3 hour(s) 13 minute(s) 56 seconds(s) ago
于 2013-05-09T06:29:25.297 回答