Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@vimalsperidian vimalsperidian commented Oct 21, 2016

good one

@JoshyFrancis

This comment has been minimized.

Copy link

@JoshyFrancis JoshyFrancis commented Jun 2, 2018

Thank you

@MrzJkl

This comment has been minimized.

Copy link

@MrzJkl 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

This comment has been minimized.

Copy link

@chaisoftsnet chaisoftsnet commented Dec 7, 2018

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