By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,719 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,719 IT Pros & Developers. It's quick & easy.

Date difference in days....

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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().
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".

Dec 14 '05 #2

P: n/a
>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

P: n/a
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

P: n/a
C.
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

P: n/a
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((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.

Ken

Dec 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.