Showing posts with label Time difference. Show all posts
Showing posts with label Time difference. Show all posts

Find relative duration of an event using PHP for given date time (seconds ago, minutes ago, hours ago, days ago, weeks ago, months ago, years ago)

You might have seen that many of the live applications shows the time relative to when it was posted as seconds ago, minutes ago, hours ago, days ago, weeks ago, month ago, year ago and so on. Instead of showing the whole date and time of any possible action on the application it is shown with smaller units, more simpler ones like a minute ago, 15 minutes and 10 seconds ago etc. Here is how we achieve it using PHP, below are two methods that can be used, it requires a Date Time string in YYYY-MM-DD HH:II:SS format.


Method One:

  /**
     * returns the time ago in string
     * @param string $date_time
     *
     * @return string
     */
    function timeAgo($date_time)
    {
        $time_ago = strtotime($date_time);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
        $str = ''; // Seconds
        if ($seconds <= 60) {
            $str = "few seconds ago";
        } // Minuteselse
        if ($minutes <= 60) {
            if ($minutes == 1) {
                $str = "one minute ago";
            } else {
                $str = "$minutes minutes ago";
            }
        } // Hours
        else if ($hours <= 24) {
            if ($hours == 1) {
                $str = "an hour ago";
            } else {
                $str = "$hours hours ago";
            }
        } // Days
        else if ($days <= 7) {
            if ($days == 1) {
                $str = "yesterday";
            } else {
                $str = "$days days ago";
            }
        } // Weeks
        else if ($weeks <= 4.3) {
            if ($weeks == 1) {
                $str = "a week ago";
            } else {
                $str = "$weeks weeks ago";
            }
        } // Months
        else if ($months <= 12) {
            if ($months == 1) {
                $str = "a month ago";
            } else {
                $str = "$months months ago";
            }
        } // Years
        else {
            if ($years == 1) {
                $str = "one year ago";
            } else {
                $str = "$years years ago";
            }
        }
        return $str;
    }
ex:
echo timeAgo('2015-01-17 14:20:00'); 
o/p: 31 minutes ago

Method Two: in detail

  /**
     * gives time ago in detailed
     *
     * @param string $date_time
     * @return string
     */
    function timeAgo($date_time)
    {
        $then = new DateTime($date_time);
        $now = new DateTime();
        $delta = $now->diff($then);
        $quantities = array(
            'year' => $delta->y,
            'month' => $delta->m,
            'day' => $delta->d,
            'hour' => $delta->h,
            'minute' => $delta->i,
            'second' => $delta->s
        );
        $str = '';
        foreach ($quantities as $unit => $value) {
            if ($value == 0)
                continue;
            $str .= $value . ' ' . $unit;
            if ($value != 1) {
                $str .= 's';
            }
            $str .= ', ';
        }
        $str = $str == '' ? 'a moment ' : substr($str, 0, - 2);
        return $str . ' ago';
    }
    
ex:
echo timeAgo('2015-01-17 14:20:00'); 
o/p: 31 minutes, 14 seconds ago