Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HUGE_DICK_10_INCHES's avatar

Get 30 or 31 days of certain month in carbon

I don't know how I can't find this anywhere on the web.

 public static function months($futureM) {
        $now = Carbon::now();
        $start    = (new DateTime($now->format('Y-m-d')))->modify('first day of this month');
        $end      = (new DateTime(($now->addMonths($futureM))->format('Y-m-d')))->modify('first day of next month');
        $interval = DateInterval::createFromDateString('1 month');
        $period   = new DatePeriod($start, $interval, $end);
        $months = array();
        foreach ($period as $dt) {
            array_push($months, array('month'=>$dt->format("F/Y"), 'days'=> ));
        }
        return $months;
    }

I need to get number of certain month days. How can I do that?

0 likes
4 replies
HUGE_DICK_10_INCHES's avatar

Oh come one, not now() - for specific month.

Like: topvillas gave me just I needed to figure out what I needed and it works:

Carbon::parse($dt->format('Y-m-d'))->daysInMonth
Snapey's avatar

Don't be so anal

I just gave you an example of using the attribute of ANY Carbon object and a link to all the attributes.

12 likes

Please or to participate in this conversation.