0

Example:

$difference = strtotime($to) - strtotime($from);
$months = ($difference / 86400 / 30 );

Problem: But this way I never get exact average. Because I can’t sure for 30 days there can be 31 and 28 days months also.

Even I tried to divide by 12 month average but that also can’t work in every month selection cases read first and change according to ur own

Sarthak Sharma
  • 679
  • 2
  • 7
  • 17

2 Answers2

0

You can get number of days for certain month in certain year using this function:

PHP Manual - cal_days_in_month

You can get number of days for whole year using, for example, this solution:

Finding the total number of days in year

Also, if you just want to get number of days for current month, you can use this:

date("t");
0

Are you after the number of months in a date range? If so, you could modify this previous answer to handle what you want:

PHP: Loop through all months in a date range?

To what I think you're after, you'd do something like this

$date_from = strtotime("2013-08-01");
$date_to = strtotime("2013-10-01");

$month_count = 0;
while ($date_from < $date_to) {
  $date_from = strtotime('+1 month', $date_from);
  $month_count++;
}

// month_count = number of months covered in the date range

Or, if you're just looking for the number of days in a date range, you could do something like this:

$date_from = strtotime("2013-08-01");
$date_to = strtotime("2013-08-28");
$diff = $date_to - $date_from;
$days_in_range = floor($diff / (60*60*24));

//days_in_range = 27

Not entirely sure what you're after from your question.

Community
  • 1
  • 1
SubjectCurio
  • 4,702
  • 3
  • 32
  • 46