0

I know it is very crazy, but I am still asking for your solution.

Now, I have two dates: let say start date: 2017-02-01 and end date: 2017-04-01.

If I compare the end date with the start date, of course I would get 60 days (includes one day of 2017-02-01 for itself).

Assume I don't know what month and how many days do the months have between these 2 dates. Erm...is there anyway to determine the days and month... clearly?

Something like this:

  1. 2017-02-01 => 2017-02-28 = 28 days AND this is February
  2. 2017-03-01 => 2017-03-31 = 31 days AND this is March
  3. 2017-04-01 => 2017-04-01 = 1 day AND this is April

I could only find the days between two dates in PHP, and of course it is easier than this one. I think this one already covers the array concept, and already beyond my IQ XD.

I need your help, prove yourselves by this solving this question, thank you so much! Much appreciated.

In simpler: What I wanted to find is -- I don't want to find the day between two dates, I wanna find what month and how many days for each month between these two dates.

P/S: I don't have any code for this, because I don't even know how to start to code this.

3 Answers3

1

You can use date_diff function. date_diff function count the day between two date, so you can add 2 to count your date too..

$date1=date_create("2017-02-01");
$date2=date_create("2017-04-01");
$diff=date_diff($date1,$date2);

$res =  $diff->format("%R%a days");
echo ($res + 2);
0

just try this

$from_date = "2017-02-01";
$to_date = "2017-04-01";
$start = $month = strtotime($from_date);
$tmp = cal_days_in_month(CAL_GREGORIAN,date('m',strtotime($to_date)),date('Y',strtotime($to_date)));
$end = strtotime(date('Y-m',strtotime($to_date)).'-'.$tmp);
while($month < $end){
    $days = cal_days_in_month(CAL_GREGORIAN,date('m',$month),date('Y',$month));
    if(date('m',$start) == date('m',$month)) {
        $days = $days - date('d',$start) + 1;
    } else if(date('m',$end) == date('m',$month)) {
        $days = date('d',strtotime($to_date));
    }
    echo "Days :".$days." Month :".date('F',$month);
    echo '<br>';
    $month = strtotime("+1 month", $month);
}

output will looks like below

Days :28 Month :February Days :31 Month :March Days :01 Month :April
Mahipal Patel
  • 543
  • 3
  • 15
-1

PHP has some quite powerful DateTime classes and functions.

<?php

$start = new DateTime('2017-05-01');
$end = new DateTime('2017-07-03');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

$output = ['daysTotal' => 0, 'months' => []];
$months = [];
foreach ($period as $dt) {
    if (!in_array($dt->format("F"), $months)) {
        $month = ['name' => $dt->format("F"), 'days' => cal_days_in_month(CAL_GREGORIAN, $dt->format("n"), $dt->format("Y"))];
        $output['months'][] = $month;
        $months[] = $dt->format("F");

    }
    $output['daysTotal']++;
}

var_dump($output);

This will output:

array(2) {
  ["daysTotal"]=>
  int(63)
  ["months"]=>
  array(3) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "May"
      ["days"]=>
      int(31)
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(4) "June"
      ["days"]=>
      int(30)
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(4) "July"
      ["days"]=>
      int(31)
    }
  }
}

Note: If you do between 2017-05-01 and 2017-06-01, it will not register June in the months array.

Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22