Jump to content

No of months between two timestamps (Unix timestamps)


nadeemshafi9

Recommended Posts

Except for simple comparisons, timestamps must be converted to be usable for most purposes. Because the number of days in the months are not constant, you would need to convert to years, months, days and do the math.

 

I would take the difference in the years * 12, plus the difference in the months (subtract 1 if the end day is less-than the start day.)

 

Untested, but should work -

 

<?php
// whole months 

// get the timestamps into year, month, day values using your preferred method -
$start = '2008-07-29';
$end = '2009-08-29';

list($sy,$sm,$sd) = explode("-",$start);
list($ey,$em,$ed) = explode("-",$end);

$months = (($ey-$sy) * 12) + ($em - $sm) - ($ed < $sd);
echo $months;
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.