Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP – Why date_diff returns unexpected result?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading