views:

209

answers:

2

I have a Date of Birth field and trying to use the timespan function to get the age, but returns "28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes".

Any idea how I can just get the "28 Years" part?

Thanks!

A: 

There are many ways to do this, with the string in $date, like so:

$date = '28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes';
This will give you "28 Years"
$yearsPart = substr($date, 0, strpos($date, 'Years') + 5);
So will this:
$parts = split(', ', $date);
$yearsPart = $parts[0];
Kevin Chan
+1  A: 

I suggest you use PHP's strftime() function. Instead of using the CI's timespan().

echo strftime('%Y', 1226239392);

Thorpe Obazee