Correctly calculate age
authorMichael Tänzer <neo@nhng.de>
Mon, 20 Jun 2011 23:12:54 +0000 (01:12 +0200)
committerMichael Tänzer <neo@nhng.de>
Mon, 20 Jun 2011 23:12:54 +0000 (01:12 +0200)
Signed-off-by: Michael Tänzer <neo@nhng.de>
manager/application/models/User.php

index 55eeadb..7b7a519 100644 (file)
@@ -168,7 +168,16 @@ class Default_Model_User {
      */
     public function getAge() {
         $now = new Zend_Date();
-        return $now->sub($this->getDob())->toValue(Zend_Date::YEAR);
+        $dob = $this->getDob();
+        $age = $now->get(Zend_Date::YEAR) - $dob->get(Zend_Date::YEAR);
+        
+        // Did we have a happy birthday already this year?
+        $dob->setYear($now);
+        if ($dob->compare($now) > 0) {
+               $age -= 1;
+        }
+        
+        return $age;
     }
     
     /**