How To Number Of Week Days Between Two Dates In PHP?

Dec 30, 2021

.

Admin

How To Number Of Week Days Between Two Dates In PHP?

Hi Friends,

This example is how to Number of week days between two dates in php?

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)

So let's start following example.

Example:


<?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

)

#PHP