Skip to content

Instantly share code, notes, and snippets.

@hh2k
Created May 1, 2019 13:15
Show Gist options
  • Save hh2k/2d79c9dba18ae0bbfcde9d43354f4f59 to your computer and use it in GitHub Desktop.
Save hh2k/2d79c9dba18ae0bbfcde9d43354f4f59 to your computer and use it in GitHub Desktop.

Revisions

  1. hh2k created this gist May 1, 2019.
    39 changes: 39 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?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>";
    }