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?>
1You are given two string (dd-mm-yyyy) representing two date,
2you have to find number of all weekdays present in between given dates.
3
4function number_of_days($days, $start, $end) {
5 $start = strtotime($start); $end = strtotime($end);
6 $w = array(date('w', $start), date('w', $end));
7 $x = floor(($end-$start)/604800);
8 $sum = 0;
9 for ($day = 0;$day < 7;++$day) {
10 if ($days & pow(2, $day)) {
11 $sum += $x + ($w[0] > $w[1]?$w[0] <= $day || $day <= $w[1] : $w[0] <= $day && $day <= $w[1]);
12 }
13 }
14 return $sum;
15}
16
17function getWeeklyDayNumbers($startDate, $endDate) {
18 $weekdays = array('monday' => 0, 'tuesday' => 0, 'wednesday' => 0, 'thursday' => 0, 'friday' => 0, 'saturday' => 0, 'sunday' => 0);
19 $weekdays['monday'] += number_of_days(0x02, $startDate, $endDate); // MONDAY
20 $weekdays['tuesday'] += number_of_days(0x04, $startDate, $endDate); // TUESDAY
21 $weekdays['wednesday'] += number_of_days(0x08, $startDate, $endDate); // WEDNESDAY
22 $weekdays['thursday'] += number_of_days(0x10, $startDate, $endDate); // THURSDAY
23 $weekdays['friday'] += number_of_days(0x20, $startDate, $endDate); // FRIDAY
24 $weekdays['saturday'] += number_of_days(0x40, $startDate, $endDate); // SATURDAY
25 $weekdays['sunday'] += number_of_days(0x01, $startDate, $endDate); // SUNDAY
26 return $weekdays;
27}
28
29$weekdays = getWeeklyDayNumbers('01-01-2021', '31-01-2021');
30print_r($weekdays);exit;
31
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');