Skip to content

Instantly share code, notes, and snippets.

@sepehr
Last active May 4, 2020 23:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sepehr/6351322 to your computer and use it in GitHub Desktop.
Save sepehr/6351322 to your computer and use it in GitHub Desktop.
PHP: Get month difference between two timestamps
<?php
/**
* Calculates how many months is past between two timestamps.
*
* @param int $start Start timestamp.
* @param int $end Optional end timestamp.
*
* @return int
*/
function get_month_diff($start, $end = FALSE)
{
$end OR $end = time();
$start = new DateTime("@$start");
$end = new DateTime("@$end");
$diff = $start->diff($end);
return $diff->format('%y') * 12 + $diff->format('%m');
}
@vimalsperidian
Copy link

good one

@JoshyFrancis
Copy link

Thank you

@MrzJkl
Copy link

MrzJkl commented Jul 18, 2018

Good one! Thank you!
The solution is not that elegant, but it´s working, so...
Have a great time!

@chaisoftsnet
Copy link

Thank you So Every Good Every Good Every Good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment