Created
October 31, 2017 15:26
-
-
Save xputerax/a0c2bf0e695ed6e587d21f6826d24a4c to your computer and use it in GitHub Desktop.
Human age to dog age converter
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 | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment