我有一个脚本,用于检查条目添加到数据库的日期和时间,将其与今天的日期和时间进行比较,并输出最相关单位(分钟,小时,天,周等)的差异。 / p>
问题是,任何超过一周的条目仅显示为“6天前添加。”
我在计算时差方面显然有一些问题,但我似乎无法识别它。
这是我的代码:
$date_time_added = $date_added . $time_added;
$current_date_time = $current_date . $current_time;
// Check how many X ago comment was added
$diff = abs(strtotime($current_date_time) - strtotime($date_time_added));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
// Check if comment was added in last 24 hours
if ($days < '1') {
// Check if comment was added in the last hour
if($hours == 0) {
// Check if comment wass added in the last minute
if ($minutes == 0) {
$when = 'Posted ' . $seconds . ' seconds ago';
} else {
$when = 'Posted ' . $minutes . ' minutes ago'; }
} else {
$when = 'Posted ' . $hours . ' hours ago';
}
} else {
$when = 'Posted ' . $days . ' days ago';
}
感谢您的帮助。
答案 0 :(得分:1)
将floor()
更改为round()
。此外,您在上次作业中拼错了$minutes
。
答案 1 :(得分:1)
为什么在计算月份时会花费多年时间?如果你只需要最相关的单位就不会这样做:
$date_time_added = $date_added . $time_added;
$current_date_time = $current_date . $current_time;
// Check how many X ago comment was added
$diff = abs(strtotime($current_date_time) - strtotime($date_time_added));
$years = floor($diff / (365*60*60*24));
$months = floor($diff / (30*60*60*24));
$days = floor($diff / (60*60*24));
$hours = floor($diff / (60*60));
$minutes = floor($diff / 60);
$seconds = $diff;
if($years==0)
{
if($months==0)
{
if($days==0)
{
if($hours==0)
{
if($minutes==0)
{
$when='Posted '.$seconds.' seconds ago';
}
else
{
$when='Posted '.$minutes.' minutes ago';
}
}
else
{
$when='Posted '.$hours.' hours ago';
}
}
else
{
$when='Posted '.$days.' days ago';
}
}
else
{
$when='Posted '.$days.' months ago';
}
}
else
{
$when='Posted '.$years.' years ago';
}
答案 2 :(得分:0)
我最终在我的一个项目中使用了Gareth的答案。这是一个修改后的版本,可以提供1天或1个月而不是1天或1个月。谢谢Gareth!
$diff = (strtotime("Now") - strtotime($date_time_added));
$years = floor($diff / (365*60*60*24));
$months = floor($diff / (30*60*60*24));
$days = floor($diff / (60*60*24));
$hours = floor($diff / (60*60));
$minutes = floor($diff / 60);
$seconds = $diff;
if($years==0)
{
if($months==0)
{
if($days==0)
{
if($hours==0)
{
if($minutes==0)
{
$when = ($seconds != 1) ? $seconds . ' seconds ago' : $seconds . ' second ago';
}
else
{
$when = ($minutes != 1) ? $minutes . ' minutes ago' : $minutes . ' minute ago';
}
}
else
{
$when = ($hours != 1) ? $hours . ' hours ago' : $hours . ' hour ago' ;
}
}
else
{
$when = ($days != 1) ? $days . ' days ago' : $days . ' day ago' ;
}
}
else
{
$when = ($months != 1) ? $months . ' months ago' : $months . ' months ago' ;
}
}
else
{
$when = ($years != 1) ? $years . ' years ago' : $years . ' years ago' ;
}