skip to Main Content

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

2

Answers


  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");
    ?>
    
    Login or Signup to reply.
  2. 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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search