1

twitterやFBのように時間形式で表示したい(3時間前投稿、2分前投稿…)

私は成功せずにこのコードを試しました:

function format_interval($timestamp, $granularity = 2) {
  $units = array('1 year|@count years' => 31536000, '1 week|@count weeks' => 604800, '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1 min|@count min' => 60, '1 sec|@count sec' => 1);
  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($timestamp >= $value) {
      $floor = floor($timestamp / $value);
      $output .= ($output ? ' ' : '') . ($floor == 1 ? $key[0] : str_replace('@count', $floor, $key[1]));
      $timestamp %= $value;
      $granularity--;
    }

    if ($granularity == 0) {
      break;
    }
}

$this->format_interval(); のような別の関数へのコールバックでこの関数を使用します。それを私のビューに渡します

私の現在の形式の日付は : 2012-07-26 09:31:pmで、既に DB に保存されています

どんな助けでも大歓迎です!

4

4 に答える 4

9

Date Helper の メソッドtimespan()は、次のことを行います。

この関数の最も一般的な目的は、過去のある時点から現在までの経過時間を表示することです。

タイムスタンプを指定すると、次の形式で経過時間が表示されます。

1 年 10 か月 2 週間 5 日 10 時間 16 分

したがって、あなたの例では、日付をタイムスタンプに変換し、次のようにするだけです。

$post_date = '13436714242';
$now = time();

// will echo "2 hours ago" (at the time of this post)
echo timespan($post_date, $now) . ' ago';
于 2012-07-30T20:05:36.107 に答える
1

ファイルで次のようなことを試してくださいmy_date_helper.php(ソース: Codeigniter Forums ):

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if( ! function_exists('relative_time'))
{
    function relative_time($datetime)
    {
        $CI =& get_instance();
        $CI->lang->load('date');

        if(!is_numeric($datetime))
        {
            $val = explode(" ",$datetime);
           $date = explode("-",$val[0]);
           $time = explode(":",$val[1]);
           $datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
        }

        $difference = time() - $datetime;
        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        if ($difference > 0) 
        { 
            $ending = $CI->lang->line('date_ago');
        } 
        else 
        { 
            $difference = -$difference;
            $ending = $CI->lang->line('date_to_go');
        }
        for($j = 0; $difference >= $lengths[$j]; $j++)
        {
            $difference /= $lengths[$j];
        } 
        $difference = round($difference);

        if($difference != 1) 
        { 
            $period = strtolower($CI->lang->line('date_'.$periods[$j].'s'));
        } else {
            $period = strtolower($CI->lang->line('date_'.$periods[$j]));
        }

        return "$difference $period $ending";
    }


} 

この形式は、データベースで使用している形式とは少し異なります (24 時間制を使用してフロントエンド用に変換するのではなく、なぜ pm/am で時刻をマークするのですか?)。いずれにせよ、それを機能させるために多くの作業は必要ありません。

于 2012-07-30T19:46:46.223 に答える
0

これを次のように解決する機能がありました。

$int_diff       =   (time() - $int_time);

$str_this_year  =   date('Y-01-01', $int_time);
$str_weekday    =   t('time_weekday_'.strtolower(date('l', $int_time)));
$str_month      =   t('time_month_'.strtolower(date('F', $int_time)));

$arr_time_formats   =   array(  '-90 seconds'   =>  t('time_a_minute_at_most'),
                                '-45 minutes'   =>  t('time_minutes_ago', ceil($int_diff / (60))),
                                '-70 minutes'   =>  t('time_an_hour_at_most'),
                                '-8 hours'      =>  t('time_hours_ago', ceil($int_diff / (60 * 60))),
                                'today'         =>  t('time_hours_ago', ceil($int_diff / (60 * 60))),
                                'yesterday'     =>  t('time_yesterday', date('H:i', $int_time)),
                                '-4 days'       =>  t('time_week_ago', $str_weekday, date('H:i', $int_time)),
                                $str_this_year  =>  t('time_date', date('j', $int_time), $str_month, date('H:i', $int_time)),
                                0               =>  t('time_date_year', date('j', $int_time), $str_month, date('Y', $int_time), date('H:i', $int_time)));

if ($boo_whole)
    return $arr_time_formats[0];

foreach(array_keys($arr_time_formats) as $h)
    if ($int_time >= strtotime($h))
        return $arr_time_formats[$h];

基本的にはとt()を組み合わせた機能です。ここでのアイデアは、フォールバックとして、最も近い時間に到達するまで実行されるキーを提供することです。$this->lang->line()sprintf()strtotime()0

見栄えの良い概観で時間を簡単に調整できるため、このアプローチは非常に優れています。コードの一部をさらに提供することもできますが、あまりにも多くの作業を行っているように感じます :) 基本的に、これはそれを行う方法の背後にある理論にすぎません。

于 2012-07-30T20:31:00.477 に答える