src/TimeLib.php
public static function get_human_readble_time_difference(\DateTime $timestamp)
    {
        $resultString = "";
        $now = new \DateTime();

        $diffInSeconds = $now->getTimestamp() - $timestamp->getTimestamp();
        
        if ($diffInSeconds < 60)
        {
            $resultString = "$diffInSeconds seconds";
        }
        elseif ($diffInSeconds < 3600)
        {
            $diffInMinutes = (int) ($diffInSeconds / 60);
            $remainder = (int) ($diffInSeconds % 60);
            $resultString = "$diffInMinutes mins $remainder secs";
        }
        elseif ($diffInSeconds < 86400)
        {
            $diffInHours = (int) ($diffInSeconds / 3600);
            $remainder = (int) ($diffInSeconds % 3600);
            $minutes = (int)($remainder / 60);
            $resultString = "$diffInHours hours $minutes mins";
        }
        else
        {
            $diffInDays = (int) ($diffInSeconds / 86400);
            $remainder = (int) ($diffInSeconds % 86400);
            $hours = (int)($remainder / 3600);
            $resultString = "$diffInDays days $hours hours";
        }
        
        return $resultString;
    }
public static function get_human_readble_time_difference(\DateTime $timestamp)
    {
        $resultString = "";
        $now = new \DateTime();

        $diffInSeconds = $now->getTimestamp() - $timestamp->getTimestamp();
        
        if ($diffInSeconds < 60)
        {
            $resultString = "$diffInSeconds seconds";
        }
        elseif ($diffInSeconds < 3600)
        {
            $diffInMinutes = (int) ($diffInSeconds / 60);
            $remainder = (int) ($diffInSeconds % 60);
            $resultString = "$diffInMinutes mins $remainder secs";
        }
        elseif ($diffInSeconds < 86400)
        {
            $diffInHours = (int) ($diffInSeconds / 3600);
            $remainder = (int) ($diffInSeconds % 3600);
            $minutes = (int)($remainder / 60);
            $resultString = "$diffInHours hours $minutes mins";
        }
        else
        {
            $diffInDays = (int) ($diffInSeconds / 86400);
            $remainder = (int) ($diffInSeconds % 86400);
            $hours = (int)($remainder / 3600);
            $resultString = "$diffInDays days $hours hours";
        }
        
        return $resultString;
    }
public static function get_human_readble_time_difference ( \ DateTime $ timestamp ) { $ resultString = "" ; $ now = new \ DateTime ( ) ; $ diffInSeconds = $ now -> getTimestamp ( ) - $ timestamp -> getTimestamp ( ) ; if ( $ diffInSeconds < 60 ) { $ resultString = "$diffInSeconds seconds" ; } elseif ( $ diffInSeconds < 3600 ) { $ diffInMinutes = ( int ) ( $ diffInSeconds / 60 ) ; $ remainder = ( int ) ( $ diffInSeconds % 60 ) ; $ resultString = "$diffInMinutes mins $remainder secs" ; } elseif ( $ diffInSeconds < 86400 ) { $ diffInHours = ( int ) ( $ diffInSeconds / 3600 ) ; $ remainder = ( int ) ( $ diffInSeconds % 3600 ) ; $ minutes = ( int ) ( $ remainder / 60 ) ; $ resultString = "$diffInHours hours $minutes mins" ; } else { $ diffInDays = ( int ) ( $ diffInSeconds / 86400 ) ; $ remainder = ( int ) ( $ diffInSeconds % 86400 ) ; $ hours = ( int ) ( $ remainder / 3600 ) ; $ resultString = "$diffInDays days $hours hours" ; } return $ resultString ; }
Given a timestamp, get how long ago it was in human readable form.
E.g. 10 seconds
4 minutes
1 hour
456 days
@param \DateTime $timestamp - the timestamp we want the difference to
@return String - a human readable string to represent how long ago the timestamp was.
Given a timestamp get how long ago it was in human readable form . E . g . 10 seconds 4 minutes 1 hour 456 days