Get all months names between two dates in carbon
Asked Answered
G

1

8

I found only this on any questions and answers out here but none are related to carbon.

Is there a way to do this in carbon:

$start    = (new DateTime('2018-06-01'))->modify('first day of this month');
    $end      = (new DateTime('2019-06-01'))->modify('first day of next month');
    $interval = DateInterval::createFromDateString('1 month');
    $period   = new DatePeriod($start, $interval, $end);

    foreach ($period as $dt) {
        echo $dt->format("Y-m") . "<br>\n";
    }
Gans answered 1/6, 2018 at 11:15 Comment(0)
I
22

Use CarbonPeriod class todo same

    use Carbon\CarbonPeriod;
    $period = CarbonPeriod::create('2018-06-01', '1 month', '2019-06-01');

    foreach ($period as $dt) {
            echo $dt->format("Y-m") . "<br>\n";
    }
Inclinatory answered 1/6, 2018 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.