Tools\Utility\Time::age PHP Метод

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

Both dates default to current date. Note that start needs to be before end for a valid result.
public static age ( integer | string $start, integer | string | null $end = null ) : integer
$start integer | string Start date (if empty, use today)
$end integer | string | null End date (if empty, use today)
Результат integer Age (0 if both timestamps are equal or empty, -1 on invalid dates)
    public static function age($start, $end = null)
    {
        if (empty($start) && empty($end) || $start == $end) {
            return 0;
        }
        if (is_int($start)) {
            $start = date(FORMAT_DB_DATE, $start);
        }
        if (is_int($end)) {
            $end = date(FORMAT_DB_DATE, $end);
        }
        $startDate = $start;
        if (!is_object($start)) {
            $startDate = new DateTime($start);
        }
        $endDate = $end;
        if (!is_object($end)) {
            $endDate = new DateTime($end);
        }
        if ($startDate > $endDate) {
            return -1;
        }
        $oDateInterval = $endDate->diff($startDate);
        return $oDateInterval->y;
    }

Usage Example

Пример #1
0
 /**
  * TimeTest::testAgeBounds()
  *
  * @return void
  */
 public function testAgeBounds()
 {
     //$this->out($this->_header(__FUNCTION__), true);
     $values = [[20, 20, ['min' => '1990-07-07', 'max' => '1991-07-06']], [10, 30, ['min' => '1980-07-07', 'max' => '2001-07-06']], [11, 12, ['min' => '1998-07-07', 'max' => '2000-07-06']]];
     foreach ($values as $v) {
         //echo $v[0].'/'.$v[1];
         $ret = $this->Time->ageBounds($v[0], $v[1], true, '2011-07-06');
         //TODO: relative time
         //pr($ret);
         if (isset($v[2])) {
             $this->assertSame($v[2], $ret);
             $this->assertEquals($v[0], $this->Time->age($v[2]['max'], '2011-07-06'));
             $this->assertEquals($v[1], $this->Time->age($v[2]['min'], '2011-07-06'));
         }
     }
 }