Jump to content

Months in selected date range.


girish.kc

Recommended Posts

I am working on a report generation. Here I need to count the number of months involved in the selected date range. I need to apply the monthly charges accordingly.

 

Example: If the user selects 25-08-2011 to 03-10-2011, it should return 3 as the number of months.

 

Yes, the number of days are less than 60 but still the date range is spread across 3 months.

 

Any solution.. Please.

 

Girish

Link to comment
Share on other sites

With a little bit thinking I came up with this function:

 

function count_months($date_from, $date_to) {
   return round((strtotime('last day of this month', strtotime($date_to)) - strtotime('first day of this month', strtotime($date_from))) / 2629743.83);
}

echo count_months('25-08-2011', '03-10-2011');

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.