1

I am running this function

function Age($month, $day, $year) {
    date_default_timezone_set('America/New_York');
    $dob = $month .''. $day .''. $year;
    $startDate = strtotime($dob);
    $endDate = time();
    $dif = $endDate - $startDate;

    return $years = (date('Y', $dif) - 1970) .' y, '. ($months = date('n', $dif) - 1).' m';
}

inside a foreach loop (some CodeIgniter markup):

foreach ($users as $row) {
    echo $this->includes->Age($row->birth_month, $row->birth_day, $row->birth_year)
  }

The loop works OK, showing all my users.

But the problem is that it calculates the age correctly for the first user and then shows that same age for all other users. I should point out that all other user's data is correct, only the age is wrong.

Anyone know how to fix this?

Thanks!

3 Answers 3

3

Your $dob looks malformed. Try $dob = $year . '-' . $month . '-' $day;

Sign up to request clarification or add additional context in comments.

1 Comment

strtotime() Doesn't like 'mm-dd-yyyy'
1

You're missing space/separators.

$dob = $month .''. $day .''. $year;

should be

$dob = $month .'/'. $day .'/'. $year;

edit: Ambiguity. If using mm-dd-yyyy format, should be /

. and - indicate dd-mm-yyyy

1 Comment

This is not necessary, but if so, the best way is use mktime
0

Maybe CI is caching the result?
Seems that function Age is correct

3 Comments

to my eyes CI is running the function (top code) and returning the values straight to the view (bottom code), so I just can't understand why it doesn't refresh the value for each loop
Oh.. try to use this foreach ($users as &$row)
&$row didn't work - tried @shad's suggestion above and fixed the issue (malformed date var)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.