1//How to calculate date different between two dates in larevel
2// METHOD-1
3$date1 = new DateTime("2018-01-10 00:00:00");
4$date2 = new DateTime("2019-05-18 01:23:45");
5$difference = $date1->diff($date2);
6$diffInSeconds = $difference->s; //45
7$diffInMinutes = $difference->i; //23
8$diffInHours = $difference->h; //8
9$diffInDays = $difference->d; //21
10$diffInMonths = $difference->m; //4
11$diffInYears = $difference->y; //1
12
13//or get Date difference as total difference
14//METHOD-2
15$d1 = strtotime("2018-01-10 00:00:00");
16$d2 = strtotime("2019-05-18 01:23:45");
17$totalSecondsDiff = abs($d1-$d2); //42600225
18$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
19$totalHoursDiff = $totalSecondsDiff/60/60;//11833.39
20$totalDaysDiff = $totalSecondsDiff/60/60/24; //493.05
21$totalMonthsDiff = $totalSecondsDiff/60/60/24/30; //16.43
22$totalYearsDiff = $totalSecondsDiff/60/60/24/365; //1.35
1<?php
2 //check if date between two dates
3$currentDate = date('Y-m-d');
4$currentDate = date('Y-m-d', strtotime($currentDate));
5$startDate = date('Y-m-d', strtotime("01/09/2019"));
6$endDate = date('Y-m-d', strtotime("01/10/2019"));
7if (($currentDate >= $startDate) && ($currentDate <= $endDate)){
8 echo "Current date is between two dates";
9}else{
10 echo "Current date is not between two dates";
11}
12//@sujay
1// how to get dates between two dates in laravel?
2
3//NOTE => for this you can use Carbon
4use Carbon\CarbonPeriod;
5
6$period = CarbonPeriod::create("2020-5-20", "2020-5-30");
7foreach ($period as $date) {
8 // Insert Dates into listOfDates Array
9 $listOfDates[] = $date->format('Y-m-d');
10}
11
12// Now You Can Review This Array
13dd($listOfDates);
14