Created
May 1, 2019 13:15
-
-
Save hh2k/2d79c9dba18ae0bbfcde9d43354f4f59 to your computer and use it in GitHub Desktop.
Calculate when an array of people is half the age of each other
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| date_default_timezone_set('Europe/Copenhagen'); | |
| $tz = new DateTimeZone('Europe/Copenhagen'); | |
| function calc($person,$person2) | |
| { | |
| $earlier = new DateTime($person["birth"]); | |
| $later = new DateTime($person2["birth"]); | |
| $days = $later->diff($earlier)->format("%a"); | |
| echo $person["name"] . " and " . $person2["name"] . "<br>"; | |
| $later->modify("+$days day"); | |
| $final = $later->format("d-m-Y"); | |
| $age1 = DateTime::createFromFormat('Y-m-d', $person["birth"], $tz)->diff(new DateTime($final))->y; | |
| $age2 = DateTime::createFromFormat('Y-m-d', $person2["birth"], $tz)->diff(new DateTime($final))->y; | |
| echo $person2["name"] . " is half as old on the " . $final . "<br>"; | |
| echo $person["name"] . " is $age1 years and " . $person2["name"] . " is $age2 years old"; | |
| echo "<br><br>"; | |
| } | |
| $persons = array( | |
| array("name" => "Name of person 1", "birth" => "1940-09-25"), | |
| array("name" => "Name of person 2", "birth" => "1960-11-09"), | |
| ); | |
| foreach ($persons as $person) { | |
| $birth = new DateTime($person["birth"]); | |
| foreach ($persons as $person2) { | |
| $birth2 = new DateTime($person2["birth"]); | |
| if ($birth == $birth2 || $birth2 < $birth) continue; | |
| calc($person, $person2); | |
| } | |
| echo "<hr>"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment