Даты не отображаются должным образом от 2-х дат в php - PullRequest
0 голосов
/ 01 мая 2018

Код ниже не показывает выходные данные месяца 05 в качестве входных данных, но если диапазон дат большой, он показывает правильную информацию.

//Code for finding All the Dates between TWO Date
//Code for finding month between two Date
$startDate = new DateTime("2018-04-26");
$inputEndDate = new DateTime("2018-05-03");
$inputEndDate->modify('+1day');//for increasing the day by 1

//for counting the month
$monthInterval = new DateInterval('P1M');
$monthPeriod   = new DatePeriod($startDate, $monthInterval, $inputEndDate);
$monthCount = 0;

foreach ($monthPeriod as $date) {
$endDate = new DateTime(date("Y-m-t", strtotime($date->format('Y-m-d')))); 

//last day of the month

$endDate->modify('+1 day');//for increasing the day by 1
if ($inputEndDate < $endDate) {
    $endDate = $inputEndDate;
}


//var_dump($endDate);

$dateRange = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$startDate = $endDate->add(new DateInterval('P1D'))->modify('-1 day');


echo "{$date->format('F')}-{$date->format('Y')}";
foreach($dateRange as $dateHeader){
    echo $dateHeader->format("Y-m-d");
}
$monthCount++;}

Выход:

April-2018
2018-04-26
2018-04-27
2018-04-28
2018-04-29
2018-04-30

Выходные данные не показывают результат другого месяца, как на входе.

1 Ответ

0 голосов
/ 01 мая 2018
function get_dates_through_range($start, $end, $format = 'Y-m-d')
{
    $range = array();
    $interval = new DateInterval('P1D');

    $range_end = new DateTime($end);
    $range_end->add($interval);

    $period = new DatePeriod(new DateTime($start), $interval, $range_end);

    foreach($period as $date) {
        $range[] = $date->format($format);
    }

    return $range;
 }

get_dates_through_range('2018-04-30','2018-05-03')

Выходы

array(4) {
    [0]=>
    string(10) "2018-04-30"
    [1]=>
    string(10) "2018-05-01"
    [2]=>
    string(10) "2018-05-02"
    [3]=>
    string(10) "2018-05-03"
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...