Habari\DateTime::difference PHP Метод

difference() публичный статический Метод

print_r( DateTime::difference( 'now', 'January 1, 2010' ) ); output (past): Array ( [invert] => [y] => 0 [m] => 9 [w] => 3 [d] => 5 [h] => 22 [i] => 33 [s] => 5 ) print_r( DateTime::difference( 'now', 'January 1, 2011' ) ); output (future): Array ( [invert] => 1 [y] => 0 [m] => 2 [w] => 0 [d] => 3 [h] => 5 [i] => 33 [s] => 11 ) If 'invert' is true, the time is in the future (ie: x from now). If it is false, the time is in the past (ie: x ago). For more information, see PHP's DateInterval class, which this and friendly() attempt to emulate for < PHP 5.3
public static difference ( mixed $start_date, mixed $end_date ) : array
$start_date mixed The start date, as a HDT object or any format accepted by DateTime::create().
$end_date mixed The end date, as a HDT object or any format accepted by DateTime::create().
Результат array Array of each interval and whether the interval is inverted or not.
    public static function difference($start_date, $end_date)
    {
        // if the dates aren't HDT objects, try to convert them to one. this lets you pass in just about any format
        if (!$start_date instanceof DateTime) {
            $start_date = DateTime::create($start_date);
        }
        if (!$end_date instanceof DateTime) {
            $end_date = DateTime::create($end_date);
        }
        $result = array();
        // calculate the difference, in seconds
        $difference = $end_date->int - $start_date->int;
        if ($difference < 0) {
            // if it's negative, time AGO
            $result['invert'] = false;
        } else {
            // if it's positive, time UNTIL
            $result['invert'] = true;
        }
        $difference = abs($difference);
        // we'll progressively subtract from the seconds left, so initialize it
        $seconds_left = $difference;
        $result['y'] = floor($seconds_left / self::YEAR);
        $seconds_left = $seconds_left - $result['y'] * self::YEAR;
        $result['m'] = floor($seconds_left / self::MONTH);
        $seconds_left = $seconds_left - $result['m'] * self::MONTH;
        $result['w'] = floor($seconds_left / self::WEEK);
        $seconds_left = $seconds_left - $result['w'] * self::WEEK;
        $result['d'] = floor($seconds_left / self::DAY);
        $seconds_left = $seconds_left - $result['d'] * self::DAY;
        $result['h'] = floor($seconds_left / self::HOUR);
        $seconds_left = $seconds_left - $result['h'] * self::HOUR;
        $result['i'] = floor($seconds_left / self::MINUTE);
        $seconds_left = $seconds_left - $result['i'] * self::MINUTE;
        $result['s'] = $seconds_left;
        return $result;
    }