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并且已经存储在我的数据库中

任何帮助将不胜感激!

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 论坛):

<?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";
    }


} 

该格式与您在数据库中使用的格式略有不同(为什么您使用 pm/am 标记时间,而不是仅使用 24 小时时间并转换为前端?)。无论哪种方式,都不应该花费太多的工作来让它工作。

于 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 回答
0
<?php 

    $this->load->helper('date');

    //client created date get from database
    $date=$client_list->created_date; 

    // Declare timestamps
    $last = new DateTime($date);
    $now = new DateTime( date( 'Y-m-d h:i:s', time() )) ; 

    // Find difference
    $interval = $last->diff($now);

    // Store in variable to be used for calculation etc
    $years = (int)$interval->format('%Y');
    $months = (int)$interval->format('%m');
    $days = (int)$interval->format('%d');
    $hours = (int)$interval->format('%H');
    $minutes = (int)$interval->format('%i');

                                 //   $now = date('Y-m-d H:i:s');


                                 if($years > 0)
                                 {
                                 echo $years.' Years '.$months.' Months '.$days.' Days '. $hours.' Hours '.$minutes.' minutes ago.' ;
                                 }
                                 else if($months > 0)
                                 {
                                 echo $months.' Months '.$days.' Days '. $hours.' Hours '.$minutes.' minutes ago.' ;
                                 }
                                 else if($days > 0)
                                 {
                                 echo $days.' Days '.$hours.' Hours '.$minutes.' minutes ago.' ;
                                 }
                                 else if($hours > 0)
                                 {
                                 echo  $hours.' Hours '.$minutes.' minutes ago.' ;
                                 }
                                 else
                                 {
                                 echo $minutes.' minutes ago.' ;
                                 }


                                 ?>
于 2018-06-11T12:27:03.217 回答