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"; }
Output for 8.2.31 - 8.2.32, 8.3.0 - 8.3.32, 8.4.1 - 8.4.23, 8.5.0 - 8.5.8
Bob was born 26 Year(s), and 21 Week(s), and 1 Day(s) ago Jane was born 21 Year(s), and 15 Week(s) ago Mark was born 14 Year(s), and 31 Week(s), and 2 Day(s) ago

preferences:
45.08 ms | 777 KiB | 5 Q