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, $join_phrase = ', and '): string { $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"; } echo PHP_EOL; foreach($people as $value) { $birthDate = new DateTime($value['bday']); echo "It's been " . timeBetweenToString(timeBetween($value['bday']), ' + ' ) . "since {$value['name']} was born on " . $birthDate->format('D, F jS ') . "\n"; }
Output for 8.2.31 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Bob was born 26 Year(s), and 21 Week(s), and 2 Day(s) ago Jane was born 21 Year(s), and 15 Week(s), and 1 Day(s) ago Mark was born 14 Year(s), and 31 Week(s), and 3 Day(s) ago It's been 26 Year(s) + 21 Week(s) + 2 Day(s)since Bob was born on Thu, February 17th It's been 21 Year(s) + 15 Week(s) + 1 Day(s)since Jane was born on Wed, March 30th It's been 14 Year(s) + 31 Week(s) + 3 Day(s)since Mark was born on Mon, December 5th

preferences:
32.15 ms | 1153 KiB | 7 Q