1

counting no. Of days between two dates inclusive of starting date is easy but how to count them saperately for each months like between 1.1.2014 and 15.5.2014

Jan 31
Feb 28
Mar 31
Apr 30
May 15
Junaid Rehman
  • 169
  • 4
  • 11

3 Answers3

3

I think you are looking for something like this:

$start = new DateTime('2014-01-01');
$end = new DateTime('2014-05-15');

$tmpStart = $start;
while ( $tmpStart <= $end ) {
    $tmpEnd = clone $tmpStart;
    $tmpEnd = $tmpEnd->modify('last day of this month');
    $tmpEnd = $tmpEnd < $end ? $tmpEnd : clone $end;
    echo $tmpStart->format("M") . " " . ($tmpEnd->diff($tmpStart)->format("%a")+1) . "\n";
    $tmpStart = $tmpEnd->modify('first day of next month');
}

This outputs:

Jan 31 
Feb 28 
Mar 31 
Apr 30 
May 15 
nl-x
  • 11,762
  • 7
  • 33
  • 61
0

get the number of days in month from cal_days_in_month() function and use date_diff() to compare between those

Karl Adler
  • 15,780
  • 10
  • 70
  • 88
0

If I understand correctly, you want to find the difference in days between two dates?

<?php
$startDate = mktime(0, 0, 0, 1, 1, 1970); //Enter your start date and time
$endDate = mktime(0, 0, 0, 5, 20, 2014); //Enter your end date and time

echo ($endDate-$startDate)/60/60/24; //This works out the number of days between the two
?>

Outputs:

16210

Look at the mktime() documentation to understand how to input your date into the mktime() function.

OdinX
  • 4,135
  • 1
  • 24
  • 33