1//get Date diff as intervals
2$d1 = new DateTime("2018-01-10 00:00:00");
3$d2 = new DateTime("2019-05-18 01:23:45");
4$interval = $d1->diff($d2);
5$diffInSeconds = $interval->s; //45
6$diffInMinutes = $interval->i; //23
7$diffInHours = $interval->h; //8
8$diffInDays = $interval->d; //21
9$diffInMonths = $interval->m; //4
10$diffInYears = $interval->y; //1
11
12//or get Date difference as total difference
13$d1 = strtotime("2018-01-10 00:00:00");
14$d2 = strtotime("2019-05-18 01:23:45");
15$totalSecondsDiff = abs($d1-$d2); //42600225
16$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
17$totalHoursDiff = $totalSecondsDiff/60/60;//11833.39
18$totalDaysDiff = $totalSecondsDiff/60/60/24; //493.05
19$totalMonthsDiff = $totalSecondsDiff/60/60/24/30; //16.43
20$totalYearsDiff = $totalSecondsDiff/60/60/24/365; //1.35
1$date1 = "2007-03-24";
2$date2 = "2009-06-26";
3
4$diff = abs(strtotime($date2) - strtotime($date1));
5
6$years = floor($diff / (365*60*60*24));
7$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
8$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
9
10printf("%d years, %d months, %d days\n", $years, $months, $days);
1// how to calculate days difference between two dates in laravel
2
3use DateTime; // inside Controller Class
4
5$startDate = new DateTime($request->start_date);
6$endDate = new DateTime($request->end_date);
7
8$daysDifference = ($startDate->diff($endDate)->days);
1phpCopy$firstDate = "2019-01-01";
2$secondDate = "2020-03-04";
3
4$dateDifference = abs(strtotime($secondDate) - strtotime($firstDate));
5
6$years = floor($dateDifference / (365 * 60 * 60 * 24));
7$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
8$days = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 *24) / (60 * 60 * 24));
9
10echo $years." year, ".$months." months and ".$days." days";
11
12//output: 1 year, 2 months and 3 days
13
1function numWeeks($dateOne, $dateTwo){
2 //Create a DateTime object for the first date.
3 $firstDate = new DateTime($dateOne);
4 //Create a DateTime object for the second date.
5 $secondDate = new DateTime($dateTwo);
6 //Get the difference between the two dates in days.
7 $differenceInDays = $firstDate->diff($secondDate)->days;
8 //Divide the days by 7
9 $differenceInWeeks = $differenceInDays / 7;
10 //Round down with floor and return the difference in weeks.
11 return floor($differenceInWeeks);
12}
13
14$numOfWeek = numWeeks('2021-01-21', '2021-01-28');