Skip to content

Instantly share code, notes, and snippets.

@grim-reapper
Forked from reinink/gist:1645193
Created October 23, 2020 16:17
Show Gist options
  • Select an option

  • Save grim-reapper/ada216f46f11b20db5a27ce02902bb90 to your computer and use it in GitHub Desktop.

Select an option

Save grim-reapper/ada216f46f11b20db5a27ce02902bb90 to your computer and use it in GitHub Desktop.
PHP function to calculate age
<?php
function calculate_age($birthday)
{
$today = new DateTime();
$diff = $today->diff(new DateTime($birthday));
if ($diff->y)
{
return ($diff->y == 1) ? $diff->y . ' year' : $diff->y . ' years';
}
elseif ($diff->m)
{
return ($diff->m == 1) ? $diff->m . ' month' : $diff->m . ' months';
}
else
{
return ($diff->d == 1) ? $diff->d . ' day' : $diff->d . ' days';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment