girish.kc Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248629-months-in-selected-date-range/ Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 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 https://forums.phpfreaks.com/topic/248629-months-in-selected-date-range/#findComment-1276845 Share on other sites More sharing options...
girish.kc Posted October 7, 2011 Author Share Posted October 7, 2011 Hi silkfire, Very quick replay.. Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/248629-months-in-selected-date-range/#findComment-1276862 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 2629743.83 is the amount of seconds in a month. The calculation return something with 2.99043234 so that's why we're rounding before returning the result. Glad I could help ya. Link to comment https://forums.phpfreaks.com/topic/248629-months-in-selected-date-range/#findComment-1276863 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.