Php 如果时间少于24小时前,则显示x时间前
我使用Codeigniter,它有timespan()函数,返回时间为Php 如果时间少于24小时前,则显示x时间前,php,codeigniter,Php,Codeigniter,我使用Codeigniter,它有timespan()函数,返回时间为1年、10个月、2周、5天、10小时、16分钟 我想做的是,如果时间在过去24小时内,只显示格式为x小时前的时间,否则只显示正常的日期时间 我觉得必须有一个函数已经做了这样做,但我没有任何运气找到它 这是Codeigniter附带的timespan函数,我如何更改它 /** * Timespan * * Returns a span of seconds in this format: * 10 days 14 ho
1年、10个月、2周、5天、10小时、16分钟
我想做的是,如果时间在过去24小时内,只显示格式为x小时前的时间,否则只显示正常的日期时间
我觉得必须有一个函数已经做了这样做,但我没有任何运气找到它
这是Codeigniter附带的timespan函数,我如何更改它
/**
* 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);
}
}
/**
*时间跨度
*
*以以下格式返回秒的范围:
*10天14小时36分47秒
*
*@access-public
*@param integer表示秒数
*@param整数Unix时间戳
*@返回整数
*/
如果(!function_存在('timespan'))
{
函数时间跨度($seconds=1,$time='')
{
$CI=&get_instance();
$CI->lang->load('date');
如果(!是数值($s))
{
$seconds=1;
}
如果(!是数值($time))
{
$time=time();
}
如果($time 0)
{
$str.=$years.'.$CI->lang->line(($years>1)?'date_years':'date_years')),';
}
$seconds-=$years*31536000;
$months=floor($seconds/2628000);
如果($year>0或$month>0)
{
如果($months>0)
{
$str.=$months.'.$CI->lang->line(($months>1)“‘日期月’:‘日期月’)”);
}
$seconds-=$months*2628000;
}
$weeks=底价($seconds/604800);
如果($year>0或$month>0或$weeks>0)
{
如果($weeks>0)
{
$str.=$weeks.'.$CI->lang->line(($weeks>1)?'date\u weeks':'date\u weeks')),';
}
$seconds-=$weeks*604800;
}
$days=下限($seconds/86400);
如果($month>0或$weeks>0或$days>0)
{
如果($days>0)
{
$str.=$days.'.$CI->lang->line(($days>1)“‘日期’:‘日期’)”);
}
$seconds-=$days*86400;
}
$hours=地板($seconds/3600);
如果($days>0或$hours>0)
{
如果($hours>0)
{
$str.=$hours.'.$CI->lang->line(($hours>1)-'date\u hours':'date\u hours')),';
}
$seconds-=$hours*3600;
}
$分钟=地板($秒/60);
如果($days>0或$hours>0或$minutes>0)
{
如果($minutes>0)
{
$str.=$minutes.'.$CI->lang->line(($minutes>1)?'date\u minutes':'date\u minutes')),';
}
$seconds-=$minutes*60;
}
如果($str='')
{
$str.=$seconds'.$CI->lang->line(($seconds>1)?“日期秒”:“日期秒”);
}
返回substr(trim($str),0,-1);
}
}
if(time()-$yourTimeif(time()-$yourTime此函数将接受字符串、数字(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
function time\u ago($time=false,$just\u now=false){
如果($time instanceOf DateTime)
$time=$time->getTimestamp();
elseif(是数字($time))
$time=日期('m/d/y h:i A',$time);
if(strotime($time)==false)
$time=日期('m/d/y h:i A',time());
$interval=date\u create($time)->diff(date\u create('now');
$形容词=strotime($time)>time()?“从现在起”:“以前”;
返回(
$interval->days>0?
$time:(
$interval->h<1&&$interval->i<1&&$just\u?
“刚才”
(
$interval->h>1?
$interval->h.“小时”(
$interval->h>1?
“s”:
''
)“以前”:
$interval->i.“分钟”。$形容词
)
)
);
}
回声时间(2012年8月22日下午5:00);//3小时前
回音时间(2012年8月21日下午5:00);//2012年8月21日下午5:00
echo time_ago(time());//0小时前
echo time_ago(time(),true);//刚才
回音时间(strotime(“5天前”);//08/17/12 08:18 PM
回声时间(strotime('5小时前);//5小时前
回声时间(strotime('5分钟前);//5分钟前
回音时间(strotime(“+5分钟”);//从现在起5分钟
echo time_ago('jQuery.now()',true);//刚才
回音时间(甜美的爆炸声,兄弟,没错);//刚才
文档
日期
-
DateTime
object-
DateInterval
object-
是数字的
-
此函数将接受字符串、数字(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
function time\u ago($time=false,$just\u now=false){
如果($time instanceOf DateTime)
$time=$time->getTimestamp();
elseif(是数字($time))
$time=日期('m/d/y h:i A',$time);
if(strotime($time)==false)
$time=日期('m/d/y h:i A',time());
$interval=date\u create($time)->diff(date\u create('now');
$形容词=strotime($time)>time()?“从现在起”:“以前”;
返回(
$interval->days>0?
$time:(
$interval->h<1&&$interval->i<1&&$just\u?
“刚才”
(
$interval->h>1?
$interval->h.“小时”(
$interval->h>1?
“s”:
<?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;
}
}