Finding days between two dates in laravel

Date loop date differance

get all dates between two days ( s_date and e_date)

use Carbon\Carbon;
use Carbon\CarbonPeriod;


     $start =Carbon::parse('2021-01-23');
$end =Carbon::parse('2021-01-25')->subDay(1);
$dateRange =CarbonPeriod::create($start, $end);

$dates = [];
foreach($dateRange as $date) {
    $dates[] = $date->format('Y-m-d');
}

dd($dates);

another way
     
$from = Carbon::parse('2021-01-23');
$to = Carbon::parse('2021-01-25');
  $dates = [];

for($d = $from;  $d->lte($to) ; $d->addDay()) {
    $dates[] = $d->format('Y-m-d');
}

//return $dates;

Others way

 
 $start = new \Carbon\Carbon('2021-01-30');
    $end = new \Carbon\Carbon('2021-02-02');
    $days = $start->diff($end)->days;

    for($i = 0; $i < $days; $i++)
    {
        $date = '';
        $date = $start->addDays();

        $blackoutDays[] = $date->format('Y-m-d');
    }
return  $blackoutDays;
      
      
      Get All Records Between Two Dates in Laravel
      Increment Php Carbon Date in for loop with Laravel
      Incrementing dates with Carbon

No comments

Powered by Blogger.