【发布时间】:2010-12-21 22:23:42
【问题描述】:
您好,我现在已经搜索了孔网并找到了很多,但我只是不知道如何使它工作,所以现在我在这里寻求帮助
我想这样做,然后一个人创建一个应该说“创建 1 秒前”的评论,然后是 1 分 1 小时,就像这样 :)
有人可以帮我吗?
谢谢
【问题讨论】:
标签: php codeigniter echo timespan
您好,我现在已经搜索了孔网并找到了很多,但我只是不知道如何使它工作,所以现在我在这里寻求帮助
我想这样做,然后一个人创建一个应该说“创建 1 秒前”的评论,然后是 1 分 1 小时,就像这样 :)
有人可以帮我吗?
谢谢
【问题讨论】:
标签: php codeigniter echo timespan
这基本上是人类可读的格式,可以通过数学检查来检查时间的距离,工作如下:sn-p:
function RelativeTime($timestamp)
{
$difference = time() - $timestamp;
$periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
if ($difference > 0)
{
$ending = "ago";
}
else
{
$difference = -$difference;
$ending = "to go";
}
for($j = 0; $difference >= $lengths[$j]; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
return $difference . $periods[$j] . $ending;
}
这将处理未来的时间戳,例如 12 days to go 以及时间戳,例如 12 days ago
希望这会有所帮助。
原文来源:http://blog.evandavey.com/2008/04/php-date-in-human-readable-form-facebook-style.html
【讨论】:
我认为这正是您想要的。使用函数时,将 $deep 参数设置为 1。
function timespan($seconds = 1, $time = '', $deep = NULL)
{
$CI = & get_instance();
$CI->lang->load('date');
$current_deep = 0;
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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$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')) . ', ';
if (++$current_deep == $deep)
return substr(trim($str), 0, -1);
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds . ' ' . $CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')) . ', ';
}
return substr(trim($str), 0, -1);
}
【讨论】:
假设您有差异$now - $creation_time 以秒为单位,一种方法是将其除以 X 秒(1 分钟 = 60、1 小时 = 3600、1 天 = 86400),从最大的数字开始,看看如何其中许多单位适合您的创建时间,然后使用其剩余部分来尝试适合较小的单位。
$diffSeconds = time() - $creation_time ;
$numDays = $diffSeconds / 86400 ;
$remainderDaySeconds = $diffSeconds % 86400 ;
$numHours = $remainderDaySeconds / 3600 ;
$remainderSeconds = $remainderDaySeconds % 3600 ;
模运算符% 将为您提供除法的余数。这样,如果一个帖子是在不到一天前创建的,那么 $numDays 是 0 和 $remainderDaySeconds 是 $diffSeconds,因此您可以相应地检查并打印出来。
编辑我很好奇并查看了 SO,结果发现有很多问题在此展开。链接一些:
Calculate relative time in C# calculating and showing a date as 'secs ago', 'mins ago', 'hours ago' etc 指向 http://www.php.net/manual/en/function.time.php#89415
【讨论】: