Tools\Utility\Time::ageBounds PHP Method

ageBounds() public static method

Get the age bounds (min, max) as timestamp that would result in the given age(s) note: expects valid age (> 0 and < 120)
public static ageBounds ( integer $firstAge, integer | null $secondAge = null, boolean $returnAsString = false, string | null $relativeTime = null ) : array
$firstAge integer
$secondAge integer | null (defaults to first one if not specified)
$returnAsString boolean
$relativeTime string | null
return array Array('min'=>$min, 'max'=>$max);
    public static function ageBounds($firstAge, $secondAge = null, $returnAsString = false, $relativeTime = null)
    {
        if ($secondAge === null) {
            $secondAge = $firstAge;
        }
        //TODO: other relative time then today should work as well
        $Date = new DateTime($relativeTime !== null ? $relativeTime : 'now');
        $max = mktime(23, 23, 59, $Date->format('m'), $Date->format('d'), $Date->format('Y') - $firstAge);
        $min = mktime(0, 0, 1, $Date->format('m'), (int) $Date->format('d') + 1, $Date->format('Y') - $secondAge - 1);
        if ($returnAsString) {
            $max = date(FORMAT_DB_DATE, $max);
            $min = date(FORMAT_DB_DATE, $min);
        }
        return ['min' => $min, 'max' => $max];
    }

Usage Example

Exemplo n.º 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'));
         }
     }
 }