Jump to content

[SOLVED] Finding number of days between dates problem


rvdb86

Recommended Posts

Hi, I have the following code to count the number of days between two dates:

strtotime($endDate) - strtotime($fixedStartDate)) / (60 * 60 * 24)

 

The problem is that when I tested it with the start date: 2009-01-01 and end date: 2009-02-10 the result was 40 when really there are 41 days?? :shrug:

 

Anyone know what i am doing wrong?

Look at the logic behind what your saying. Take it as day of the year. 01/01 is the first of the year so we'll take it as 1. 02/10 is the tenth day of the second month. The first month (January) has 31 days, making the 10th of February the 41st day of the year (the 10 in February plus the 31 from January).

 

So we now have 41 - 1 which unless I'm mistaken is 40. Basically the maths you are doing says on the 41st day of the year 40 days have passed. For calculating inclusive time periods you'll have to add one to the result because you are including both the first and last day.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.