0

Basically I was trying to get age from date of birth, for which I am using Computed Field module.

Following code was used:

$isoArray = explode ( '-', substr($entity->field_dob1[0]['value'],0,10));
$bdayunix = mktime ( 00, 00, 00, (int)$isoArray[1], (int)$isoArray[2], (int)$isoArray[0]);

$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$entity_field[0]['value'] = $age;

When I checked the code, I found that this part of code is not giving the correct result:

 $isoArray = explode ( '-', substr($entity->field_dob1[0]['value'],0,10));
 $bdayunix = mktime ( 00, 00, 00, (int)$isoArray[1], (int)$isoArray[2], (int)$isoArray[0]);

I can say that because when I try to convert $bdayunix into date,whatever the date of birth is, I always get the result 1999.

ie; $dob = date("Y",$bdayunix ); always gives result:1999.

Could you please tell me where I am going wrong? Is this problem of Computed field module or I am doing some mistake in my code ?

(Date format in users bday field is yyyy-mm-dd and I have tried all types: Date, DateISO format and Date Unix timestamp).

Thank you in advance.

0

I found the solution .

If we select Field type as Date(Unix Timestamp) then $entity->field_dob1[0]['value'] will return unix timestamp value which can be directly used.

So if anyone in same requirement can use the following code:

$bdayunix = $entity->field_date_of_birth['und'][0]['value'];
$nowunix = time();
$unixage = $nowunix - $bdayunix;
$age = floor($unixage/ (365 * 24 * 60 * 60));

$entity_field[0]['value'] = $age;

PS: Dont forget to select field type as Date(Unix Timestamp).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.