Helper::getAge PHP Method

getAge() public static method

If date of death provided, then returns age at point of death
public static getAge ( string $dob, string $date_of_death = null, string $check_date = null ) : string
$dob string
$date_of_death string
$check_date string Optional date to check age at (default is today)
return string $age
    public static function getAge($dob, $date_of_death = null, $check_date = null)
    {
        if (!$dob) {
            return 'Unknown';
        }
        $dob_datetime = new DateTime($dob);
        $check_datetime = new DateTime($check_date);
        if ($date_of_death) {
            $dod_datetime = new DateTime($date_of_death);
            if ($check_datetime->diff($dod_datetime)->invert) {
                $check_datetime = $dod_datetime;
            }
        }
        return $dob_datetime->diff($check_datetime)->y;
    }

Usage Example

Example #1
0
 public function getProfile()
 {
     $db = new DB();
     $sql = "SELECT * FROM " . self::$profile_table . " WHERE id=:id";
     $profile = $db->query($sql, [':id' => $this->id]);
     foreach ($profile[0] as $key => $val) {
         if ('id' == $key) {
             continue;
         }
         $this->{$key} = $val;
     }
     $this->age = Helper::getAge($this->birthday);
 }
All Usage Examples Of Helper::getAge