2012-07-30 40 views
1

我想顯示像Twitter和FB時間格式(發表於3小時前,發佈2分鐘前等等...)顯示「很久以前」,而不是在PHP笨日期時間

我已經試圖這段代碼沒有成功:

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與已存儲在我的數據庫

任何幫助將非常感激!

回答

7

Date Helper'stimespan()方法,只是做的是:

此功能的最常見的目的是爲了顯示多少時間,現在已經從一些點起過去的時間過去。

給出一個時間戳,它會顯示多少時間在這個格式已經過去:

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'; 
+0

這樣做的竅門,感謝您的幫助;) – user990463 2012-07-31 10:36:29

+0

@ user990463:不客氣!我很高興這很有幫助。 – 2012-07-31 13:15:19

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小時時間並轉換爲前端?)。無論哪種方式,不應該花費太多的工作才能使其工作。

0

我有AF結該解決了這個像這樣:

$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]; 

Basicly t()$this->lang->line()sprintf()組合的函數。這裏的想法是給出通過strtotime()運行的密鑰,直到達到最接近的時間,0作爲回退。

這種方法非常好,因爲您可以通過一個很好的概覽輕鬆調整時間。我可以給出更多的代碼,但感覺就像做了太多的工作:)基本上,這只是你如何做到這一點的理論。

相關問題