Answers for "carbon get total week of the month"

0

carbon get total week of the month

Route::get('/enter_month_num/{month}',function($month){
    $year = Carbon::now()->year;
    $date = Carbon::createFromDate($year,$month);
    $numberOfWeeks = floor($date->daysInMonth / Carbon::DAYS_PER_WEEK);
    $start = [];
    $end = [];
    $j=1;
    for ($i=1; $i <= $date->daysInMonth ; $i++) {
        Carbon::createFromDate($year,$month,$i); 
        $start['Week: '.$j.' Start Date']= (array)Carbon::createFromDate($year,$month,$i)->startOfWeek()->toDateString();
        $end['Week: '.$j.' End Date']= (array)Carbon::createFromDate($year,$month,$i)->endOfweek()->toDateString();
        $i+=7;
        $j++; 
    }
    $result = array_merge($start,$end);
    $result['numberOfWeeks'] = ["$numberOfWeeks"];
    return $result;
});
Posted by: Guest on June-21-2021

Browse Popular Code Answers by Language