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$startDate = new DateTime("2019-10-27");
2$endDate = new DateTime("2020-04-11");
3
4$difference = $endDate->diff($startDate);
5echo $difference->format("%a");
1<?php
2function dateDifference($start_date, $end_date)
3{
4 // calulating the difference in timestamps
5 $diff = strtotime($start_date) - strtotime($end_date);
6
7 // 1 day = 24 hours
8 // 24 * 60 * 60 = 86400 seconds
9 return ceil(abs($diff / 86400));
10}
11
12// start date
13$start_date = "2016-01-02";
14
15// end date
16$end_date = "2016-01-21";
17
18// call dateDifference() function to find the number of days between two dates
19$dateDiff = dateDifference($start_date, $end_date);
20
21echo "Difference between two dates: " . $dateDiff . " Days ";
22?>
1$from_date ='01-01-2013';
2$to_date ='05-01-2013';
3
4$from_date = new DateTime($from_date);
5$to_date = new DateTime($to_date);
6
7for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
8 echo $date->format('l') . "\n";
9}
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$period = new DatePeriod(
2 new DateTime('2010-10-01'),
3 new DateInterval('P1D'),
4 new DateTime('2010-10-05')
5);
6
7//Which should get you an array with DateTime objects.
8
9//To iterate
10
11foreach ($period as $key => $value) {
12 //$value->format('Y-m-d')
13}