Use the model's afterFind() method to add the age to your data. Also,
your calculation is not very robust. Try this:
function afterFind($results)
{
if (isset($results[$this->alias]['birthdate']))
{
$results[$this->alias]['age'] =
$this->getAge($this->alias]['birthdate']);
}
return $results;
}
function getAge($dob)
{
list($y, $m, $d) = explode('-', $dob);
$year_diff = date('Y') - $y;
if (date('Y').$m.$d > date('Ymd')) $year_diff--;
return $year_diff;
}
The above assumes that birthdate field is in the form 'YYYY-mm'dd'.
On Tue, Jun 16, 2009 at 8:51 AM, Saibot<[email protected]> wrote:
>
> Hi,
>
> Suppose you have a model class 'Person' which has a attribute
> 'birthdate' and a method 'get_age()'.
> function get_age() {
> return (int)date('Y') - self.birthdate;
> }
>
> How can I call the get_age() method in my Person views?
>
> Thanks in advance.
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---