PHP中计算时差的问题

发布于 2024-10-31 10:42:07 字数 1573 浏览 0 评论 0原文

我有一个脚本,用于检查将条目添加到数据库中的日期和时间,将其与今天的日期和时间进行比较,并以最相关的单位(分钟、小时、天、周等)输出差异。

问题是任何超过一周的条目仅显示为“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';
    }

感谢您的帮助。

I have a script which checks the date and time an entry was added to the database, compares it to todays date and time, and outputs the difference in the most relevant unit (minutes, hours, days, weeks etc).

The problem is that any entries that are over a week old only display as 'added 6 days ago.'

I obviously have some problem in the way that I'm calculating the time difference but I can't seem to identify it.

Here's my code:

    $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';
    }

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几度春秋 2024-11-07 10:42:07

为什么计算月份时要把年份去掉?如果您只需要最相关的单位,这不会做到这一点:

$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';
}

Why do you take the years out when you calculate the months? If you only need the most relevant unit wouldn't this do it:

$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';
}
凉世弥音 2024-11-07 10:42:07

floor() 更改为 round()。另外,您在上次作业中拼错了 $mines

Change floor() to round(). Also, you misspelled $minutes in the last assignment.

要走就滚别墨迹 2024-11-07 10:42:07

我最终在我的一个项目中使用了加雷斯的答案。这是一个修改版本,将为您提供 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' ;
                                }  

I ended up using Gareth's answer in one of my projects. Here is a modified version that will give you 1 day or 1 month instead of 1 days or 1 months. Thanks 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' ;
                                }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文