how do i calculate number of days between two dates in php code example
Example 1: Get the number of days between two dates in PHP
$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");
$difference = $endDate->diff($startDate);
echo $difference->format("%a");
Example 2: 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 3: php calculate weeks between two dates
function numWeeks($dateOne, $dateTwo){
$firstDate = new DateTime($dateOne);
$secondDate = new DateTime($dateTwo);
$differenceInDays = $firstDate->diff($secondDate)->days;
$differenceInWeeks = $differenceInDays / 7;
return floor($differenceInWeeks);
}
$numOfWeek = numWeeks('2021-01-21', '2021-01-28');