laravel between two dates 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
$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;
$diffInMinutes = $difference->i;
$diffInHours = $difference->h;
$diffInDays = $difference->d;
$diffInMonths = $difference->m;
$diffInYears = $difference->y;
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2);
$totalMinutesDiff = $totalSecondsDiff/60;
$totalHoursDiff = $totalSecondsDiff/60/60;
$totalDaysDiff = $totalSecondsDiff/60/60/24;
$totalMonthsDiff = $totalSecondsDiff/60/60/24/30;
$totalYearsDiff = $totalSecondsDiff/60/60/24/365;
Example 3: laravel date between
$from = date('2018-01-01');
$to = date('2018-05-02');
Reservation::whereBetween('reservation_from', [$from, $to])->get();
Example 4: check if date between two dates laravel
<?php
$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";
}
Example 5: how to get dates between two dates in laravel
use Carbon\CarbonPeriod;
$period = CarbonPeriod::create("2020-5-20", "2020-5-30");
foreach ($period as $date) {
$listOfDates[] = $date->format('Y-m-d');
}
dd($listOfDates);