日期应该是大致的时间

时间:2013-05-09 05:49:50

标签: php date

我正在使用此代码进行日期

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

得到的结果如下: enter image description here

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

5 个答案:

答案 0 :(得分:3)

这将计算从您的日期过去的天数

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

echo $newdate . ' days ago.';

这将输出

491 days ago.

答案 1 :(得分:2)

看看这个很棒的库PHP carbon Library

答案 2 :(得分: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);

          ?>

DEMO AT IDEONE

<强> DEMO 2

<强> DEMO 3

答案 3 :(得分: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

答案 4 :(得分:1)

以最小单位获得时差(为了论证,让我们说几秒钟)。

然后得到一个限制,告诉你何时从一个近似步骤到下一个近似步骤。

E.g。

<?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。1年前是“一年前”。您可以修改算法,以便将余数乘以$scale并在下面的单位中进行渲染,以便1。5年变为“一年零六个月”。这也使得它有时有点尴尬,因为9天变成“一周和两天前”。

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