PHP | Number of week days between two dates - GeeksforGeeks

20 Oct, 2018
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)
Examples:
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’).
Algorithm :
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.
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.
 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.
<?php    // input start and end date    $startDate = "01-01-2018";    $endDate = "01-01-2019";          $resultDays = array('Monday' => 0,    'Tuesday' => 0,    'Wednesday' => 0,    'Thursday' => 0,    'Friday' => 0,    'Saturday' => 0,    'Sunday' => 0);      // change string to date time object    $startDate = new DateTime($startDate);    $endDate = new DateTime($endDate);      // iterate over start to end date    while($startDate <= $endDate ){        // find the timestamp value of start date        $timestamp = strtotime($startDate->format('d-m-Y'));          // find out the day for timestamp and increase particular day        $weekDay = date('l', $timestamp);        $resultDays[$weekDay] = $resultDays[$weekDay] + 1;          // increase startDate by 1        $startDate->modify('+1 day');    }      // print the result    print_r($resultDays);?>
Output :
Array
(
    [Monday] => 53
    [Tuesday] => 53
    [Wednesday] => 52
    [Thursday] => 52
    [Friday] => 52
    [Saturday] => 52
    [Sunday] => 52
)

date-time-program
PHP-date-time
PHP
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
PHP in_array() Function
How to convert array to string in PHP ?
How to pop an alert message box using PHP ?
How to delete an array element based on key in PHP?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | strval() Function
Comparing two dates in PHP