ExpressiveDate::getRelativeDate PHP Method

getRelativeDate() public method

Get a relative date string, e.g., 3 days ago.
public getRelativeDate ( ExpressiveDate $compare = null ) : string
$compare ExpressiveDate
return string
    public function getRelativeDate($compare = null)
    {
        if (!$compare) {
            $compare = new ExpressiveDate(null, $this->getTimezone());
        }
        $units = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
        $values = array(60, 60, 24, 7, 4.35, 12);
        // Get the difference between the two timestamps. We'll use this to cacluate the
        // actual time remaining.
        $difference = abs($compare->getTimestamp() - $this->getTimestamp());
        for ($i = 0; $i < count($values) and $difference >= $values[$i]; $i++) {
            $difference = $difference / $values[$i];
        }
        // Round the difference to the nearest whole number.
        $difference = round($difference);
        if ($compare->getTimestamp() < $this->getTimestamp()) {
            $suffix = 'from now';
        } else {
            $suffix = 'ago';
        }
        // Get the unit of time we are measuring. We'll then check the difference, if it is not equal
        // to exactly 1 then it's a multiple of the given unit so we'll append an 's'.
        $unit = $units[$i];
        if ($difference != 1) {
            $unit .= 's';
        }
        return $difference . ' ' . $unit . ' ' . $suffix;
    }

Usage Example

        case 1:
            // playing
            $playbackStateLabel = Label::success('Now Playing');
            if (Authority::can('manage', 'playlist', $playlist->id)) {
                $controls = Button::link_xs_normal(route('playlist.item.pause', array('playlist' => $playlist->id, 'playlistItem' => $playlistItem->id)), '', array('title' => 'Pause this item'))->with_icon('pause');
                //$controls .= Button::xs_normal('', array('title' => 'Skip this item'))->with_icon('step-forward');
            } else {
                //$controls = Button::xs_normal('', array('title' => 'Vote to skip this item'))->with_icon('step-forward');
            }
            break;
        case 2:
            // paused
            $playbackStateLabel = Label::info('Paused');
            if (Authority::can('manage', 'playlist', $playlist->id)) {
                $controls = Button::link_xs_normal(route('playlist.item.pause', array('playlist' => $playlist->id, 'playlistItem' => $playlistItem->id)), '', array('title' => 'Unpause this item'))->with_icon('play');
            }
            break;
        case 3:
            // skipped
            $playbackStateLabel = Label::info('Skipped');
            break;
    }
    $tableBody[] = array('user' => '<a class="pull-left" href="' . URL::route('user.show', $user->id) . '">' . HTML::userAvatar($user) . ' ' . e($user->username) . '</a>', 'title' => e($playlistItem->title), 'state' => $playbackStateLabel, 'controls' => $controls, 'duration' => $duration->shortFormat(), 'submitted' => $submitted->getRelativeDate());
}
?>
	{{ Table::body($tableBody) }}
	{{ Table::close() }}
	{{ $playlistItems->links() }}
@else
	No playlist entries to show!
@endif