Tools\Utility\Time::ageByYear PHP Method

ageByYear() public static method

Returns the age only with the year available can be e.g. 22/23
public static ageByYear ( integer $year, integer | null $month = null ) : integer | string
$year integer
$month integer | null (optional)
return integer | string Age
    public static function ageByYear($year, $month = null)
    {
        if ($month === null) {
            $maxAge = static::age(mktime(0, 0, 0, 1, 1, $year));
            $minAge = static::age(mktime(23, 59, 59, 12, 31, $year));
            $ages = array_unique([$minAge, $maxAge]);
            return implode('/', $ages);
        }
        if (date('n') == $month) {
            $maxAge = static::age(mktime(0, 0, 0, $month, 1, $year));
            $minAge = static::age(mktime(23, 59, 59, $month, static::daysInMonth($year, $month), $year));
            $ages = array_unique([$minAge, $maxAge]);
            return implode('/', $ages);
        }
        return static::age(mktime(0, 0, 0, $month, 1, $year));
    }

Usage Example

示例#1
0
 /**
  * TimeTest::testAgeByYear()
  *
  * @return void
  */
 public function testAgeByYear()
 {
     //$this->out($this->_header(__FUNCTION__), true);
     // year only
     $is = $this->Time->ageByYear(2000);
     //$this->out($is);
     $this->assertEquals(date('Y') - 2001 . '/' . (date('Y') - 2000), $is);
     $is = $this->Time->ageByYear(1985);
     $this->assertEquals(date('Y') - 1986 . '/' . (date('Y') - 1985), $is);
     // with month
     if (($month = date('n') + 1) <= 12) {
         $is = $this->Time->ageByYear(2000, $month);
         //$this->out($is);
         //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
         $this->assertSame(date('Y') - 2001, $is);
         //null, '2000/'.$month
     }
     if (($month = date('n') - 1) >= 1) {
         $is = $this->Time->ageByYear(2000, $month);
         //$this->out($is);
         //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
         $this->assertSame(date('Y') - 2000, $is);
         //null, '2000/'.$month)
     }
 }