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;
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.