You are given two string (dd-mm-yyyy) representing two date, you have to find number of all weekdays present in between given dates.(both inclusive)
Input : startDate = "01-01-2018" endDate = "01-03-2018" Output : Array ( [Monday] => 9 [Tuesday] => 9 [Wednesday] => 9 [Thursday] => 9 [Friday] => 8 [Saturday] => 8 [Sunday] => 8 ) Input : startDate = "01-01-2018" endDate = "01-01-2018" Output : Array ( [Monday] => 1 [Tuesday] => 0 [Wednesday] => 0 [Thursday] => 0 [Friday] => 0 [Saturday] => 0 [Sunday] => 0 )
The basic idea to find out number of all weekdays is very simple, you have to iterate from start date to end date and increase the count of each day. Hence you can calculate the desired result.
In php we can find day for a particular date by using date() function as date(‘l’, $timestamp) where $timestamp is the value of strtotime($date). Also you can increase the date value by 1 with the help of $date->modify(‘+1 day’).
- convert startDate & endDate string into Datetime() object.
- Iterate from startDate to endDate in a loop:
- Find out then timestamp of startDate.
- Find out the weekday for current timestamp of startDate.
- Increase the value of particular weekday by 1.
- Increase the value of startDate by 1.
- Print the array containing value of all weekdays.
Array ( [Monday] => 53 [Tuesday] => 53 [Wednesday] => 52 [Thursday] => 52 [Friday] => 52 [Saturday] => 52 [Sunday] => 52 )
- Find number of days between two given dates
- Program to find the number of days between two dates in PHP
- Python program to find number of days between two given dates
- Program to convert given number of days in terms of Years, Weeks and Days
- Return all dates between two dates in an array in PHP
- Date after adding given number of days to the given date
- Comparing two dates in PHP
- How to sort an array of dates in C/C++?
- Comparing dates in Python
- How to calculate the difference between two dates in PHP?
- Sort an array of dates in PHP
- How to get first day of week in PHP?
- C++ program for Sorting Dates using Selection Sort
- How to add days to $Date in PHP?
- Find day of the week for a given date
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.