Jack Roscoe
Jack Roscoe

Reputation: 4313

Trouble calculating time difference in PHP

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.

Upvotes: 0

Views: 2091

Answers (3)

jmarquis
jmarquis

Reputation: 1

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

Upvotes: 0

Gareth
Gareth

Reputation: 5719

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

Upvotes: 1

Andrea Spadaccini
Andrea Spadaccini

Reputation: 12651

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

Upvotes: 1

Related Questions