Date difference in days....

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Probably being a little thick here, but when you subtract one date away  
from another, how do you convert the resultant value into a number of  
days... I guess I could easily / 60 / 60 / 24... but that seems  
barbaric... Anything neater?


Ps, Im also just trying to work out how to calculate the number of  
mondays and tuesdays etc between two dates... Just thought I'd run this  
theory past you if I may to gain more confidence.

I figure getting the date difference, and knowing that the starting date  
falls on a Thursday holds the key... working out the days between these  
two dates should be easy enough to divide by 7 to get the whole number  
and remainder (is that a mod?)... we can just say there's x number of  
Wednesdays, x number of Thursdays etc, and then just loop through the  
remainder which is a small number so not much of system load?

then we can work out how many of those days were working days, subtract  
holiday dates from each count... and... well, that's my thoughts so far...


Re: Date difference in days....

Simon Dean wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Check these out:

I got the links from the manual page for date().
Other than checking they're not dead links I haven't tested them.
Right now it's the second 'User Contributed Note' from "aidan at php
dot net".

Re: Date difference in days....

The Pear package is not exactly well documented.

These all seem to rely on Unix timestamps - which are only cover a
relatively small interval (32 bits 1970 - 2038 IIRC).

Since most the stuff I write is talking to a DBMS anyway I usually use
the MySQL date manipulation functions. These include a datediff()



Re: Date difference in days....

Quoted text here. Click to load it

First off, you need to nail down what answer you want a little
more.  You will not always get agreement on these:

1.  How many days are there between January 1 and January 2?
My answer:  NONE, those two days are next to each other, and  
therefore none of them are Mondays regardless of what year it is.

2.  How many days are there between January 2 and January 2 (of
the same year)?  My answer:  this doesn't really make sense, but
if you insist, negative one.

If, on the other hand, you're trying to figure out how many work
days there are between the morning of date X and the evening of date Y,
some people might consider the answers to the above questions as
2 and 1, respectively.

Quoted text here. Click to load it

I had occasion to do some calculations to determine the next <day
of week> or first <day of week> in <month> after the given date.
Your calculations will likely involve a lot of % 7 or / 7 operations,
occasionally with adding 7 to avoid negative numbers being fed to
%.  You can probably avoid a loop.

                    Gordon L. Burditt

Re: Date difference in days....

Gordon Burditt wrote:
Quoted text here. Click to load it

Inclusive of the start day and end day.

Quoted text here. Click to load it


I figure somehow, will need to somewhat ignore the time, maybe get the  
two dates, set the time to the same value, then work out the day  
difference and add one to get the result I want...

Still though, don't know how to convert the result of $date1 - $date2 to  
something more useful, like, minutes, hours, or days.... That's the only  
stumbling block as far as Im concerned, Im sure I can work out the other  


Re: Date difference in days....

Simon Dean wrote (in part):
Quoted text here. Click to load it

This the simpliest function I could come up with for getting the
difference between two dates. Note that I didn't code any error

echo date_diff ("20051210", "20051230");

function date_diff($s,$e)
return((strtotime($e) - strtotime($s))/86400);

Since the strtotime() function returns the number of seconds since
1/1/1970, you need to divide the result by the number of seconds in a
day (86400) to get the number of days.


Site Timeline