get data between two dates in laravel code example

Example 1: laravel between dates

$from = date('2018-01-01');
$to = date('2018-05-02');

Reservation::whereBetween('reservation_from', [$from, $to])->get();

Example 2: how to calculate date difference between two dates in laravel

//How to calculate date different between two dates in larevel
// METHOD-1
$date1 = new DateTime("2018-01-10 00:00:00");
$date2 = new DateTime("2019-05-18 01:23:45");
$difference = $date1->diff($date2);
$diffInSeconds = $difference->s; //45
$diffInMinutes = $difference->i; //23
$diffInHours   = $difference->h; //8
$diffInDays    = $difference->d; //21
$diffInMonths  = $difference->m; //4
$diffInYears   = $difference->y; //1

//or get Date difference as total difference
//METHOD-2
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2); //42600225
$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
$totalHoursDiff   = $totalSecondsDiff/60/60;//11833.39
$totalDaysDiff    = $totalSecondsDiff/60/60/24; //493.05
$totalMonthsDiff  = $totalSecondsDiff/60/60/24/30; //16.43
$totalYearsDiff   = $totalSecondsDiff/60/60/24/365; //1.35

Example 3: check if date between two dates laravel

<?php 
  //check if date between two dates
$currentDate = date('Y-m-d');
$currentDate = date('Y-m-d', strtotime($currentDate));   
$startDate = date('Y-m-d', strtotime("01/09/2019"));
$endDate = date('Y-m-d', strtotime("01/10/2019"));   
if (($currentDate >= $startDate) && ($currentDate <= $endDate)){   
  echo "Current date is between two dates";
}else{    
  echo "Current date is not between two dates";  
}
//@sujay

Example 4: how to get dates between two dates in laravel

// how to get dates between two dates in laravel?

//NOTE => for this you can use Carbon
use Carbon\CarbonPeriod;

$period = CarbonPeriod::create("2020-5-20", "2020-5-30");
foreach ($period as $date) {
  // Insert Dates into listOfDates Array
  $listOfDates[] = $date->format('Y-m-d');
}

// Now You Can Review This Array
dd($listOfDates);

Tags:

Php Example