Jenssegers\Date\Date::timespan PHP Method

timespan() public method

Gets the timespan between this date and another date.
public timespan ( Date $time = null, string | DateTimeZon\DateTimeZone $timezone = null ) : integer
$time Date
$timezone string | DateTimeZon\DateTimeZone
return integer
    public function timespan($time = null, $timezone = null)
    {
        // Get translator
        $lang = $this->getTranslator();
        // Create Date instance if needed
        if (!$time instanceof self) {
            $time = new static($time, $timezone);
        }
        $units = ['y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'];
        // Get DateInterval and cast to array
        $interval = (array) $this->diff($time);
        // Get weeks
        $interval['w'] = (int) ($interval['d'] / 7);
        $interval['d'] = $interval['d'] % 7;
        // Get ready to build
        $str = [];
        // Loop all units and build string
        foreach ($units as $k => $unit) {
            if ($interval[$k]) {
                $str[] = $lang->transChoice($unit, $interval[$k], [':count' => $interval[$k]]);
            }
        }
        return implode(', ', $str);
    }