Jump to content

[SOLVED] Finding number of days between dates problem


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.