Skip to content

Instantly share code, notes, and snippets.

@xputerax
Created October 31, 2017 15:26
Show Gist options
  • Select an option

  • Save xputerax/a0c2bf0e695ed6e587d21f6826d24a4c to your computer and use it in GitHub Desktop.

Select an option

Save xputerax/a0c2bf0e695ed6e587d21f6826d24a4c to your computer and use it in GitHub Desktop.

Revisions

  1. xputerax created this gist Oct 31, 2017.
    30 changes: 30 additions & 0 deletions human_to_dog.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php

    function human_to_dog_years($human_age) {

    if (!$human_age || $human_age < 0) {
    throw new InvalidArgumentException('Only positive input is accepted!');
    }

    $firstTwoHumanYear = 10.5; // 10.5 dog years
    $oneHumanYear = $firstTwoHumanYear / 2; // 5.25 dog years
    $dogAge = 0;

    if ($human_age < 2) {
    $dogAge = $human_age * $oneHumanYear;
    } else {
    $dogAge = $firstTwoHumanYear + ($human_age - 2) * 4;
    }

    return $dogAge;

    }

    if (count($argv) < 2) {
    exit(sprintf("Usage: %s <age in human year>", $argv[0]));
    }

    $human_age = $argv[1];
    $dog_age = human_to_dog_years($human_age);

    echo sprintf("%s human year(s) in dog years: %s", $human_age, $dog_age);