473,598 Members | 3,183 Online

Date difference in days....

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?

Cheers
Simon

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

Workable?
Dec 14 '05 #1
5 3246
Simon Dean wrote:
... subtract one date away from another
... convert [...] into a number of days
... calculate the number of mondays and tuesdays etc between two dates <snip> Workable?

Check these out:
http://aidan.dotgeek.org/repos/?file=Duration.php
http://pear.php.net/package/Date/
http://pecl.php.net/package/date_time

I got the links from the php.net manual page for date().
Right now it's the second 'User Contributed Note' from "aidan at php
dot net".

Dec 14 '05 #2
>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?
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.
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?

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

Dec 15 '05 #3
Gordon Burditt wrote:
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?

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.

Inclusive of the start day and end day.

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.
1.

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.

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
bits.

Cheers
Simon
Dec 15 '05 #4
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()
function.

HTH

C.

Dec 15 '05 #5
Simon Dean wrote (in part):
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?

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

<?php
echo date_diff ("20051210", "20051230") ;

function date_diff(\$s,\$e )
{
return((strtoti me(\$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.

Ken

Dec 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.