-2

i am beginner at php. I want to calculate exact total number of days between two dates using php, mysql and want to show it in html form. I tried datediff but it doesn't works as it gives , diffrence not total number of days.

Date fm - 10-10-22; Date to - 20-10-22; Total Days - 11

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a working [mcve], which can be tested by others. – Progman Oct 30 '22 at 11:15

2 Answers2

1

add + 1 to date_diff the output will 11 be like :

<?php
$d1=date_create("2022-10-10");
$d2=date_create("2022-10-20");
$diff=date_diff($d1,$d2);
echo $result = 1 + $diff->format("%R%a days");
?>
Omer
  • 11
  • 3
1

php has inbuilt DateTime functions to get the difference. Below outputs 10 day difference.

$date = new DateTime('2022-10-20');
$next_date = new DateTime('2022-10-30');
echo $date->diff($next_date)->days;