我有一个脚本,检查一个条目添加到数据库的日期和时间,将其与今天的日期和时间进行比较,并以最相关的单位(分钟,小时,天,周等)输出差异。
问题是任何超过一周的条目只显示为"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';
}
谢谢你的帮助
为什么在计算月份时要把年去掉呢?如果你只需要最相关的单位,这不是可以吗?
$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';
}
将floor()
改为round()
。还有,你在上次作业中拼错了$minutes
。
我最终在我的一个项目中使用了Gareth的答案。这是一个修改后的版本,将给你1天或1个月而不是1天或1个月。谢谢加雷斯!
$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' ;
}