Skip to content

Instantly share code, notes, and snippets.

@cygeorgel
Created August 16, 2018 15:17
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 cygeorgel/636f8d5bd6b9e95a0d980eaf68040b35 to your computer and use it in GitHub Desktop.
Save cygeorgel/636f8d5bd6b9e95a0d980eaf68040b35 to your computer and use it in GitHub Desktop.
function prorataTemporis($from = '2018-06-15', $to = '2018-08-01')
{
$from = new \DateTime($from);
$to = new \DateTime($to);
$diff = $to->diff($from);
$months = $diff->y * 12 + $diff->m;
return $months + (1 / $from->format('t') * $diff->d);
}
print $this->prorataTemporis('2016-06-01') . PHP_EOL;
// 26
print $this->prorataTemporis('2018-06-01') . PHP_EOL;
// 2
print $this->prorataTemporis('2018-07-15') . PHP_EOL;
// 0.54838709677419
print $this->prorataTemporis('2018-07-31') . PHP_EOL;
// 0.032258064516129
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment