3v4l.org

run code in 500+ PHP versions simultaneously
<?php function timeBetween(string $dob): array { $DAYS_IN_YEAR = 365; $DAYS_IN_WEEK = 7; $now = new DateTime(); // now $birthDate = new DateTime($dob); $totalDays = $now->diff($birthDate)->days; return array( 'Year(s)' => (int)floor($totalDays/$DAYS_IN_YEAR), 'Week(s)' => (int)floor(($totalDays % $DAYS_IN_YEAR) / $DAYS_IN_WEEK), 'Day(s)' => (int)($totalDays % $DAYS_IN_YEAR) % $DAYS_IN_WEEK ); } function timeBetweenToString(array $timeBetween): string { $JOIN_PHRASE = ', and '; $timeBetween = array_filter($timeBetween); $result = array_reduce(array_keys($timeBetween), function ($results, $key) use ($timeBetween) { $value = $timeBetween[$key]; $results[] = "$value $key"; return $results; }, []); return implode($JOIN_PHRASE, $result); } $people = []; $people[] = array('name' => 'Bob', 'bday' => '2000-02-17'); $people[] = array('name' => 'Jane', 'bday' => '2005-03-30'); $people[] = array('name' => 'Mark', 'bday' => '2011-12-05'); foreach($people as $value) { echo "{$value['name']} was born " . timeBetweenToString(timeBetween($value['bday'])) . " ago\n"; }

preferences:
41.4 ms | 777 KiB | 5 Q