Get between two dates of all days for each month in PHP

How can I get all the days for every month between two days in an array like

$dateStart = "2016/12/14"; 
$dateFin =   "2017/04/21"

[2016/12/14 - 2016/12/31] => 17 days
[2017/01/01 - 2017/01/31] => 31 days
[2017/02/01 - 2017/02/28] => 28 days
[2017/03/01 - 2017/03/30] => 31 days
[2017/04/01 - 2017/04/21] => 21 days

      

+3


source to share


4 answers


You can use cal_days_in_month function plus DateTime class:

<?php
$dateStart = new DateTime("2016/12/14");
$dateFin = new DateTime("2017/04/21");
$firstDay = $dateStart->format('Y/m/d');
$lastDay = $dateStart->format('Y/m/t');
$totalMonths = $dateStart->diff($dateFin)->m + ($dateStart->diff($dateFin)->y*12);
$result = [];
for ($i = 0; $i <= $totalMonths; $i++)
{
    if ($i != 0){
        $dateStart->modify('first day of next month');
        $firstDay = $dateStart->format('Y/m/d');
        $dateStart->modify('last day of month');
        $lastDay = $dateStart->format('Y/m/t');
    }

    $nextDate = explode('/', $firstDay);

    $totalDays = cal_days_in_month(CAL_GREGORIAN, $nextDate[1], $nextDate[2]);
    if ($i == 0){
        $totalDays -= $dateStart->format('d');
    } else if ($i == $totalMonths) {
        $totalDays = $dateFin->format('d');
    }

    $result["$firstDay - $lastDay"] = $totalDays;
}

var_dump($result);

      

Can't improve but gives what you asked for.



Improved routine, check below:

$dateStart = new DateTime("2016/12/14");
$dateFin = new DateTime("2017/04/21");
$totalMonths = $dateStart->diff($dateFin)->m + ($dateStart->diff($dateFin)->y*12);
$result = [];
for ($i = 0; $i <= $totalMonths; $i++)
{
    if ($i != 0){
        $obj = $dateStart->modify('first day of next month');
    }

    $firstDay = $dateStart->format('Y/m/d');
    if ($i == $totalMonths){
        $lastDay = $dateFin->format('Y/m/d');
    } else {
        $lastDay = $dateStart->format('Y/m/t');
    }
    $firstDayObj = strtotime($firstDay);
    $lastDayObj = strtotime($lastDay);
    $totalDays = (int) ceil(($lastDayObj - $firstDayObj) / 86400);
    $totalDays = ((int) $dateStart->format('d') == 1) ? $totalDays + 1 : $totalDays;
    $result["$firstDay - $lastDay"] = $totalDays;
}

var_dump($result);

//array(5) { ["2016/12/14 - 2016/12/31"]=> int(17) ["2017/01/01 - 2017/01/31"]=> int(31) ["2017/02/01 - 2017/02/28"]=> int(28) ["2017/03/01 - 2017/03/31"]=> int(31) ["2017/04/01 - 2017/04/21"]=> int(21) }

      

+2


source


To get the date difference in days for two dates, you can do this:

function daysDiff($d1, $d2) 
{
    $x1 = days($d1);
    $x2 = days($d2);

    if ($x1 && $x2) {
        return abs($x1 - $x2);
    }
}

function days(DateTime $x) 
{
    $y = $x->format('Y') - 1;
    $days = $y * 365;
    $z = (int)($y / 4);
    $days += $z;
    $z = (int)($y / 100);
    $days -= $z;
    $z = (int)($y / 400);
    $days += $z;
    $days += $x->format('z');

    return $days;
}

      



$ d1 and $ d2 must be of type DateTime, I copy this code here: http://php.net/manual/es/function.date-diff.php#117691 and made a slight modification to use the hint type instead of checking the type of the variable with get_class

0


source


Powerful function to get two date differences

Check the demo here

function dateDifference($date_1, $date_2, $differenceFormat = '%a')
{
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $interval = date_diff($datetime1, $datetime2);

    return $interval->format($differenceFormat);
}

      

0


source


Try following code snippet as a solution according to the above description

$start_date = new DateTime('2016/12/14');
$start_date=$start_date->modify('+1 day');
$end_date = new DateTime('2017/04/21');
$end_date = $end_date->modify('+1 day');

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start_date, $interval ,$end_date);
$month_days=array();
foreach($daterange as $date)
{
  $date_month=$date->format('m');
  $month_days[$date_month][]=$date->format('Y/m/d');
}
$result=array();
foreach($month_days as $days)
{
  $begin=reset($days);
  $end=end($days);
  $result[$begin.'-'.$end]=count($days);
}
echo '<pre>';
print_r($result);

      

0


source







All Articles