如果时间小于24小时前显示x时间

时间:2012-08-22 23:54:19

标签: php codeigniter

我使用Codeigniter,它有timespan()函数,返回时间为1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes

我想做的只是显示在x小时前格式化的时间,如果时间在过去24小时内,否则只显示正常的日期时间。

我觉得必须有一个已经完成的功能,但我找不到任何运气。

这是Codeigniter附带的时间跨度函数,我该如何改变呢?

/**
 * Timespan
 *
 * Returns a span of seconds in this format:
 *  10 days 14 hours 36 minutes 47 seconds
 *
 * @access  public
 * @param   integer a number of seconds
 * @param   integer Unix timestamp
 * @return  integer
 */
if ( ! function_exists('timespan'))
{
    function timespan($seconds = 1, $time = '')
    {
        $CI =& get_instance();
        $CI->lang->load('date');

        if ( ! is_numeric($seconds))
        {
            $seconds = 1;
        }

        if ( ! is_numeric($time))
        {
            $time = time();
        }

        if ($time <= $seconds)
        {
            $seconds = 1;
        }
        else
        {
            $seconds = $time - $seconds;
        }

        $str = '';
        $years = floor($seconds / 31536000);

        if ($years > 0)
        {
            $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
        }

        $seconds -= $years * 31536000;
        $months = floor($seconds / 2628000);

        if ($years > 0 OR $months > 0)
        {
            if ($months > 0)
            {
                $str .= $months.' '.$CI->lang->line((($months   > 1) ? 'date_months' : 'date_month')).', ';
            }

            $seconds -= $months * 2628000;
        }

        $weeks = floor($seconds / 604800);

        if ($years > 0 OR $months > 0 OR $weeks > 0)
        {
            if ($weeks > 0)
            {
                $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
            }

            $seconds -= $weeks * 604800;
        }

        $days = floor($seconds / 86400);

        if ($months > 0 OR $weeks > 0 OR $days > 0)
        {
            if ($days > 0)
            {
                $str .= $days.' '.$CI->lang->line((($days   > 1) ? 'date_days' : 'date_day')).', ';
            }

            $seconds -= $days * 86400;
        }

        $hours = floor($seconds / 3600);

        if ($days > 0 OR $hours > 0)
        {
            if ($hours > 0)
            {
                $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
            }

            $seconds -= $hours * 3600;
        }

        $minutes = floor($seconds / 60);

        if ($days > 0 OR $hours > 0 OR $minutes > 0)
        {
            if ($minutes > 0)
            {
                $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
            }

            $seconds -= $minutes * 60;
        }

        if ($str == '')
        {
            $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
        }

        return substr(trim($str), 0, -1);
    }
}

3 个答案:

答案 0 :(得分:2)

if( time() - $yourTime <= 86400 ) {  // 86400 seconds in a day
    echo timespan($yourTime);
} else { 
   echo date('m/d/Y \a\t H:i:s', $yourTime);
}

不要依赖任何框架,特别是坏框架!

答案 1 :(得分:2)

此函数将接受字符串,数字(unix)时间戳或DateTime对象。它还接受jQuery.now()。时间可能是未来或过去。

function time_ago($time=false, $just_now=false) {
    if ($time instanceOf DateTime)
        $time = $time->getTimestamp();
    elseif (is_numeric($time))
        $time = date('m/d/y h:i A', $time);
    if (strtotime($time) === false)
        $time = date('m/d/y h:i A', time());
    $interval =  date_create($time)->diff(date_create('now'));
    $adjective = strtotime($time) > time() ? 'from now' : 'ago';
    return (
        $interval->days > 0 ? 
            $time : (
                $interval->h < 1  && $interval->i < 1 && $just_now ? 
                    'just now' : 
                    (
                        $interval->h > 1 ? 
                            $interval->h.' hour'.(
                                $interval->h > 1 ? 
                                    's' : 
                                    ''
                            ).' ago' : 
                            $interval->i.' minutes'.' '.$adjective
                    )
            )
    );
}


echo time_ago('8/22/2012 5:00 PM'); // 3 hours ago
echo time_ago('8/21/2012 5:00 PM'); // 8/21/2012 5:00 PM
echo time_ago(time()); // 0 hours ago
echo time_ago(time(), true); // just now
echo time_ago(strtotime('5 days ago')); // 08/17/12 08:18 PM
echo time_ago(strtotime('5 hours ago')); // 5 hours ago
echo time_ago(strtotime('5 minutes ago')); // 5 minutes ago
echo time_ago(strtotime('+5 minutes')); // 5 minutes from now

echo time_ago('jQuery.now()', true); // just now
echo time_ago('sweet explosions, bro!', true); // just now

<强>文档

答案 2 :(得分:1)

提供$格式以满足您的需求。几乎可以接受任何输入。

<?php

/**
 * RelativeTime - pretty printed
 * @author Dejan Marjanovic
 */
class Site5_RelativeTime
{

    private $interval = '';

    public function __construct()
    {
        call_user_func_array(array($this, 'calculate'), func_get_args());
    }

    public function calculate($start, $end = NULL)
    {

        if ( empty($start))
            return false;

        if (empty($end))
            $end = time();

        if ( ! is_numeric($start))
            $start = strtotime($start);

        if ( ! is_numeric($end))
            $end = strtotime($end);        

        if($start > $end)
            $future = TRUE;

        $start = '@' . $start;
        $end = '@' . $end;

        if ( ! ($start instanceof DateTime))
            $start = new DateTime($start);

        if ($end === null)
            $end = new DateTime();

        if ( ! ($end instanceof DateTime))
            $end = new DateTime($end);

        $interval = $end->diff($start);

        $get_plural = function($int, $str)
        {
            return $int > 1? $str.'s': $str;
        };

        $format = array();

        if ($interval->y !== 0)
            $format[] = "%y " . $get_plural($interval->y, "year");
        if ($interval->m !== 0)
            $format[] = "%m " . $get_plural($interval->m, "month");
        if ($interval->d !== 0)
            $format[] = "%d " . $get_plural($interval->d, "day");
        if ($interval->h !== 0)
            $format[] = "%h " . $get_plural($interval->h, "hour");
        if ($interval->i !== 0)
            $format[] = "%i " . $get_plural($interval->i, "minute");


        if ($interval->s !== 0)
        {
            if ( ! count($format))
            {
                $this->interval = "less than a minute";
                return;
            }
            else
            {
                $format[] = "%s " . $get_plural($interval->s, "second");
            }
        }

        if (count($format) > 1)
        {
            $format = array_shift($format) . " and " . array_shift($format);
        }
        else
        {
            $format = array_pop($format);
        }

        $tense = ($future === TRUE)? 'from now': 'ago';

        $this->interval = $interval->format($format) . ' ' . $tense;
    }

    public function __toString()
    {
        return $this->interval;
    }

}