How can PHP be used to calculate percentage based on two dates?

To calculate the percentage based on two dates in PHP, you can first calculate the difference in days between the two dates. Then, you can calculate the total number of days in a year and use the formula (difference in days / total days in a year) * 100 to get the percentage.

$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-03-15');

$diff = $date1->diff($date2);
$totalDaysInYear = date('z', strtotime('Dec 31'));

$percentage = ($diff->days / $totalDaysInYear) * 100;

echo "Percentage between the two dates: " . round($percentage, 2) . "%";