0

このコードを日付に使用したWebサイトで作業しています

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

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 に答える
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 に答える