Skip to content

Instantly share code, notes, and snippets.

@xputerax
Created October 31, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xputerax/a0c2bf0e695ed6e587d21f6826d24a4c to your computer and use it in GitHub Desktop.
Save xputerax/a0c2bf0e695ed6e587d21f6826d24a4c to your computer and use it in GitHub Desktop.
Human age to dog age converter
<?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