2012-07-30 38 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作为回退。

这种方法非常好,因为您可以通过一个很好的概览轻松调整时间。我可以给出更多的代码,但感觉就像做了太多的工作:)基本上,这只是你如何做到这一点的理论。

相关问题