src/DateTimeUtil.php
public static function getRelTime($datetime, $datetimeFrom = null)
    {
        $ret = '';
        $dateTime = self::getDateTime($datetime);
        $dateTimeFrom = self::getDateTime($datetimeFrom);
        $dateTimeSameDay = clone $dateTimeFrom;
        $dateTimeSameDay->setTime(0, 0, 0);
        $diffSec = $dateTimeFrom->getTimestamp() - $dateTime->getTimestamp();
        if ($dateTime > $dateTimeFrom) {
            // Future
            $dateTimeTest = clone $dateTimeSameDay;
            if ($dateTime < $dateTimeTest->modify('tomorrow')) {
                $ret = 'Today at '.$dateTime->format('g:ia');
            } elseif ($dateTime < $dateTimeTest->modify('+2 days')) {
                $ret = 'Tomorrow at '.$dateTime->format('g:ia');
            } else {
                $ret = $dateTime->format('F jS');
            }
        } elseif ($diffSec < 60) {
            // within a min
            $ret = 'Just now';
        } elseif ($diffSec < 3600) {
            // within an hour
            $ret = \round(($diffSec)/60) .' min ago';
        } elseif ($diffSec < 3600*6) {
            // within 6 hours
            $hrs = \round(($diffSec)/3600);
            $ret = $hrs.' '.Str::plural($hrs, 'hour').' ago';
        } elseif ($dateTime > $dateTimeSameDay) {             // since midnight so it's today
            $ret = 'Today at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400) {
            $ret = 'Yesterday at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400*7) {
            $ret = $dateTime->format('l \a\t g:ia');
        } elseif ($dateTime->getTimestamp() > \mktime(0, 0, 0, 1, 1)) {// since 1st Jan so it's this year
            $ret = $dateTime->format('F jS');
        } else {                                // last year..
            $ret = $dateTime->format('F jS, Y');
        }
        $ret = \str_replace(' at 12:00am', '', $ret);
        return $ret;
    }
public static function getRelTime($datetime, $datetimeFrom = null)
    {
        $ret = '';
        $dateTime = self::getDateTime($datetime);
        $dateTimeFrom = self::getDateTime($datetimeFrom);
        $dateTimeSameDay = clone $dateTimeFrom;
        $dateTimeSameDay->setTime(0, 0, 0);
        $diffSec = $dateTimeFrom->getTimestamp() - $dateTime->getTimestamp();
        if ($dateTime > $dateTimeFrom) {
            // Future
            $dateTimeTest = clone $dateTimeSameDay;
            if ($dateTime < $dateTimeTest->modify('tomorrow')) {
                $ret = 'Today at '.$dateTime->format('g:ia');
            } elseif ($dateTime < $dateTimeTest->modify('+2 days')) {
                $ret = 'Tomorrow at '.$dateTime->format('g:ia');
            } else {
                $ret = $dateTime->format('F jS');
            }
        } elseif ($diffSec < 60) {
            // within a min
            $ret = 'Just now';
        } elseif ($diffSec < 3600) {
            // within an hour
            $ret = \round(($diffSec)/60) .' min ago';
        } elseif ($diffSec < 3600*6) {
            // within 6 hours
            $hrs = \round(($diffSec)/3600);
            $ret = $hrs.' '.Str::plural($hrs, 'hour').' ago';
        } elseif ($dateTime > $dateTimeSameDay) {             // since midnight so it's today
            $ret = 'Today at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400) {
            $ret = 'Yesterday at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400*7) {
            $ret = $dateTime->format('l \a\t g:ia');
        } elseif ($dateTime->getTimestamp() > \mktime(0, 0, 0, 1, 1)) {// since 1st Jan so it's this year
            $ret = $dateTime->format('F jS');
        } else {                                // last year..
            $ret = $dateTime->format('F jS, Y');
        }
        $ret = \str_replace(' at 12:00am', '', $ret);
        return $ret;
    }
public static function getRelTime ( $ datetime , $ datetimeFrom = null ) { $ ret = '' ; $ dateTime = self :: getDateTime ( $ datetime ) ; $ dateTimeFrom = self :: getDateTime ( $ datetimeFrom ) ; $ dateTimeSameDay = clone $ dateTimeFrom ; $ dateTimeSameDay -> setTime ( 0 , 0 , 0 ) ; $ diffSec = $ dateTimeFrom -> getTimestamp ( ) - $ dateTime -> getTimestamp ( ) ; if ( $ dateTime > $ dateTimeFrom ) { // Future $ dateTimeTest = clone $ dateTimeSameDay ; if ( $ dateTime < $ dateTimeTest -> modify ( 'tomorrow' ) ) { $ ret = 'Today at ' . $ dateTime -> format ( 'g:ia' ) ; } elseif ( $ dateTime < $ dateTimeTest -> modify ( '+2 days' ) ) { $ ret = 'Tomorrow at ' . $ dateTime -> format ( 'g:ia' ) ; } else { $ ret = $ dateTime -> format ( 'F jS' ) ; } } elseif ( $ diffSec < 60 ) { // within a min $ ret = 'Just now' ; } elseif ( $ diffSec < 3600 ) { // within an hour $ ret = \ round ( ( $ diffSec ) / 60 ) . ' min ago' ; } elseif ( $ diffSec < 3600 * 6 ) { // within 6 hours $ hrs = \ round ( ( $ diffSec ) / 3600 ) ; $ ret = $ hrs . ' ' . Str :: plural ( $ hrs , 'hour' ) . ' ago' ; } elseif ( $ dateTime > $ dateTimeSameDay ) { // since midnight so it's today $ ret = 'Today at ' . $ dateTime -> format ( 'g:ia' ) ; } elseif ( $ dateTime -> getTimestamp ( ) >= $ dateTimeSameDay -> getTimestamp ( ) - 86400 ) { $ ret = 'Yesterday at ' . $ dateTime -> format ( 'g:ia' ) ; } elseif ( $ dateTime -> getTimestamp ( ) >= $ dateTimeSameDay -> getTimestamp ( ) - 86400 * 7 ) { $ ret = $ dateTime -> format ( 'l \a\t g:ia' ) ; } elseif ( $ dateTime -> getTimestamp ( ) > \ mktime ( 0 , 0 , 0 , 1 , 1 ) ) { // since 1st Jan so it's this year $ ret = $ dateTime -> format ( 'F jS' ) ; } else { // last year.. $ ret = $ dateTime -> format ( 'F jS, Y' ) ; } $ ret = \ str_replace ( ' at 12:00am' , '' , $ ret ) ; return $ ret ; }
Return a "3 hours ago" type string for passed timestamp

@param mixed $datetime     DateTime, string, or timestamp
@param mixed $datetimeFrom (default = now) DateTime, string, or timestamp

@return string
Return a 3 hours ago type string for passed timestamp