Jump to content

Date difference issues


ShadowMage

Recommended Posts

Hi I have an issue with a script I've written. The script calculates the number of weekdays (M-F) between two given dates.The problem is I've encountered a date range that doesn't work properly. Start date = 3/5/2010 End date = 3/25/2010When I give it that date range it always returns 11 instead of 12. Yet 3/1/2010 to 3/29/2010 returns 20 like its supposed to (I've tried lots of other dates that all work two)Here's the script, can anybody see anything that might be an issue?

$dateTimeBegin = strtotime($dateTimeBegin);if($dateTimeBegin === -1) {	return("..begin date Invalid");}if ($dateTimeEnd) {	$dateTimeEnd = strtotime($dateTimeEnd);	if($dateTimeEnd === -1) {		return("..end date Invalid");	}} else {	$dateTimeEnd = strtotime("now");}$dif = $dateTimeEnd - $dateTimeBegin;$days = floor($dif / 86400);$weeks = floor($days / 7); // Complete weeks$first_day = date("w", $dateTimeBegin);$days_remainder = floor($days % 7);$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?if ($odd_days > 7) { // Sunday	$days_remainder--;}if ($odd_days > 6) { // Saturday	$days_remainder--;}return (($weeks * 5) + $days_remainder);

Thanks!

Link to comment
Share on other sites

I'm not sure what you're using $odd_days for, but this is the code I tested with. When I did not set the timezone on top, it set $dateTimeBegin and $dateTimeEnd to two different time zones, I'm not sure why. So, the difference was a remainder of a day. At the bottom of the code you can see another way to count that up, there are actually 15 weekdays between those dates.

<?phpdate_default_timezone_set('America/Phoenix');$dateTimeBegin = '3/5/2010';$dateTimeEnd = '3/25/2010';$dateTimeBegin = strtotime($dateTimeBegin);if($dateTimeBegin === -1) {	echo("..begin date Invalid");}if ($dateTimeEnd != '') {	$dateTimeEnd = strtotime($dateTimeEnd);	if($dateTimeEnd === -1) {		echo("..end date Invalid");	}} else {	$dateTimeEnd = strtotime("now");}echo 'Start: ' . date('r', $dateTimeBegin) . '<br>';echo 'End: ' . date('r', $dateTimeEnd) . '<br>';$dif = $dateTimeEnd - $dateTimeBegin;echo 'Difference: ' . $dif . '<br>';$days = floor($dif / 86400);echo 'Days: ' . $days . '<br>';$weeks = floor($days / 7); // Complete weeksecho 'Full weeks: ' . $weeks . '<br>';$first_day = date("w", $dateTimeBegin);echo 'First day: ' . $first_day . '<br>';$days_remainder = floor($days % 7);echo 'Days left: ' . $days_remainder . '<br>';$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?echo 'Odd days: ' . $odd_days . '<br>';if ($odd_days > 7) { // Sunday	$days_remainder--;}if ($odd_days > 6) { // Saturday	$days_remainder--;}echo (($weeks * 5) + $days_remainder) . ' days<br><br>';// another way to calculate$days = 0;$cur = $dateTimeBegin;while ($cur <= $dateTimeEnd){  $day = date('w', $cur);  echo date('r', $cur) . '<br>';  if ($day != 0 && $day != 6)	$days++;  $cur += 86400;}echo $days . ' days';?>

Link to comment
Share on other sites

I'm not sure what you're using $odd_days for, but this is the code I tested with. When I did not set the timezone on top, it set $dateTimeBegin and $dateTimeEnd to two different time zones, I'm not sure why. So, the difference was a remainder of a day. At the bottom of the code you can see another way to count that up, there are actually 15 weekdays between those dates.
$odd_days is supposed to be used to check if there is a saturday or sunday in the remainder. I don't think it's working properly though. Not sure. Anyway I guess the timezone thing is what's throwing it off then huh? Any ideas on how to fix that? I also noticed I had a typo in my first post. The two dates I was using were 3/5 and 3/23 so there should be 12 days. But at least you saw the discrepancy when you tested too.I tried this method:
<?php// another way to calculate$days = 0;$cur = $dateTimeBegin;while ($cur <= $dateTimeEnd){  $day = date('w', $cur);  echo date('r', $cur) . '<br>';  if ($day != 0 && $day != 6)	$days++;  $cur += 86400;}echo $days . ' days';?>

and it seems to add an extra day to a lot of the other date ranges. For example, 3/19 to 3/30 returns 8 days when it should be 7. Yet 3/8 to 3/17 returns 7 days like its supposed to.I'm stumped.EDIT:If I change the number of seconds from 86400 to 86164 then it works perfectly. Since a day isn't exactly 24 hours it isn't exactly 86400 seconds either. 86164 is the exact length of a day. Seems odd to me that the dates in PHP would work this way. But I did notice when printing the dates out that it is indeed subtracting 3 min 56 sec off of the next day. For example:Tue, 16 Mar 2010 00:00:00 -0500Tue, 16 Mar 2010 23:56:04 -0500EDIT 2:Well, not perfectly after all. It's only near perfect. The date range 3/3 to 3/23 returns 15 instead of 14. This is really perplexing. :)

Link to comment
Share on other sites

$odd_days is supposed to be used to check if there is a saturday or sunday in the remainder. I don't think it's working properly though. Not sure.
It doesn't really check for which day it is though, it just counts the number of days. If you have a remainder of 5 days how do you know which days those are? My loop at the bottom there counts the specific number of Saturdays and Sundays.
and it seems to add an extra day to a lot of the other date ranges. For example, 3/19 to 3/30 returns 8 days when it should be 7. Yet 3/8 to 3/17 returns 7 days like its supposed to.I'm stumped.
Make sure you're setting the timezone manually.
Link to comment
Share on other sites

Well I'm not sure what timezone I should be using. Since the PHP manual says we're not supposed to use US/Central I used America/Chicago since that was the closest I could find to central Wisconsin.At any rate, setting the timezone didn't make any difference.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...