find number of weekends between two dates php code example
Example 1: how to calculate days between two dates in php
<?php
function dateDifference($start_date, $end_date)
{
$diff = strtotime($start_date) - strtotime($end_date);
return ceil(abs($diff / 86400));
}
$start_date = "2016-01-02";
$end_date = "2016-01-21";
$dateDiff = dateDifference($start_date, $end_date);
echo "Difference between two dates: " . $dateDiff . " Days ";
?>
Example 2: how to find two date under how many mondays find in php
<?php
$startDate = "01-10-2020";
$endDate = "31-10-2020";
$resultDays = array('Monday' => 0,
'Tuesday' => 0,
'Wednesday' => 0,
'Thursday' => 0,
'Friday' => 0,
'Saturday' => 0,
'Sunday' => 0);
$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);
while($startDate <= $endDate ){
$timestamp = strtotime($startDate->format('d-m-Y'));
$weekDay = date('l', $timestamp);
$resultDays[$weekDay] = $resultDays[$weekDay] + 1;
$startDate->modify('+1 day');
}
print_r($resultDays);
$totaldays = 0;
foreach ($resultDays as $key => $value)
{
if($key == "Monday")
$totaldays += $value;
if($key == "Friday")
$totaldays += $value;
}
echo $totaldays;
?>
//@sujay