skip to Main Content

I want to get the number of days between the two dates.

// DD-MM-YYYY
$date[0] = "01-03-2023";
$date[1] = "30-03-2023";

echo date_diff(date_create($date[0]),date_create($date[1]))->d + 1;

The program above returns 2, instead of the right number of days.

2

Answers


  1. Try with this code :

    $date[0] = "01-03-2023";
    $date[1] = "30-03-2023";
    
    $diff = date_diff(date_create($date[0]), date_create($date[1]));
    $days = $diff->format('%a') + 1;
    
    echo $days;
    

    or

    $date[0] = "01-03-2023";
    $date[1] = "30-03-2023";
    echo date_diff(date_create($date[0]),date_create($date[1]))->days + 1;
    

    Result :

    30
    
    Login or Signup to reply.
  2. You should call date_diff()->days which returns the number of days between the 2 dates.

    Please change:

    echo date_diff(date_create($date[0]),date_create($date[1]))->d + 1;
    

    To this:

    echo date_diff(date_create($date[0]),date_create($date[1]))->days + 1;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search