RE: [PHP] Finding # of weekdays between 2 dates..
| From: | Chad Day | Date: | Fri, 27 Dec 2002 14:15:50 +0000 |
| Subject: | RE: [PHP] Finding # of weekdays between 2 dates.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-129439@lists.php.net to get a copy of this message | ||
I should have specified, those dates were in US form.. so:
$nowdate = mktime(0, 0, 0, 1, 4, 2002);
$futuredate = mktime(0, 0, 0, 2, 5, 2002);
$futuredate - $nowdate equals 2764800
((2764800 / 86400) / 7) = 4.57142
then when floored, comes out to 4.
The only thing I can think of possibly getting around it is setting
futuredate to mktime(23, 59, 59, $month, $day, $year), but I still come up
short .. those two timestamps, after all the math before the floor(), equals
4.71, which still results in a 4. I've tried ceil and round, but with
similar inaccuracies in other dates (going 1 day too far in some cases with
ceil).
Perhaps this function I found is just not the right way to go about it, but
if anyone has any more insight, it's greatly appreciated.
Chad
-----Original Message-----
From: Marek Kilimajer [mailto:kilimajer@webglobe.sk]
Sent: Friday, December 27, 2002 2:48 AM
To: Chad Day
Cc: php general
Subject: Re: [PHP] Finding # of weekdays between 2 dates..
WFM:
$nowdate = mktime(0, 0, 0, 4, 1, 2002);
$futuredate = mktime(0, 0, 0, 5, 2, 2002);
echo weekdaysBetween ($nowdate,$futuredate,2);
I get 5
Chad Day wrote:
>I found this function in the list archives while searching on how to find
>the number of a weekday (say, Tuesdays) between 2 dates..
>
> function weekdaysBetween ($timestamp1, $timestamp2, $weekday)
> {
> return floor(intval(($timestamp2 - $timestamp1) / 86400) / 7)
> + ((date('w', $timestamp1) <= $weekday) ? 1 : 0);
> }
>
>Sometimes it works, but I've come across a date while testing this that
>doesn't work, which makes me think there is some bug in the function that
>I'm not seeing.
>
>The 2 dates I am trying are:
>
>01-04-2002
>and
>02-05-2002
>
>There should be 5 Tuesdays:
>
>01-08-2002
>01-15-2002
>01-22-2002
>01-29-2002
>02-05-2002
>
>Yet the script only returns 4, leaving off 02-05-2002.
>
> $nowdate = mktime(0, 0, 0, $STARTMONTH, $STARTDAY, $STARTYEAR);
> $futuredate = mktime(0, 0, 0, $ENDMONTH, $ENDDAY, $ENDYEAR);
>
>That's how I'm generating the time stamps to pass to the function, and I've
>confirmed the dates I've entered are correct..
>
> echo weekdaysBetween($nowdate, $futuredate, 2);
>
>Returns 4 ..
>
>
>can anyone assist?
>
>Thanks,
>Chad
>
>
>
>