Hey guys need some help on this one. I need a function that will return the number of days between two dynamic dates.
I then need to expand it to display the number of saturdays and the number of sundays in this date range as well.
Havn't a clue where to start with this one
I mean getting the number of days is easy using a timestamp the problem is finding the number of saturdays and number of sundays
Paramiliar added 42 Minutes and 20 Seconds later...
I have eventually come up with this but as you can imagine it takes a while to run (0.2 seconds to be precise) and with the query I will be running it will take even longer to run if anyone can improve on it let me know
PHP Code:function CheckHolidays($thisDay) {
$arrTargetDate=getdate($thisDay);
// Calculate Public Holidays
@sqlconnect();
// $conn = auth_db_connect();
$holiday_sql = "SELECT date FROM public_holidays WHERE date = FROM_UNIXTIME($thisDay)";
$result = mysql_query($holiday_sql);
$holiday = mysql_fetch_row($result);
@mysql_free_result($result);
if ($holiday) {
if($debug) {
echo "<small><b>".date("d.m.y",$thisDay)." is a Holiday!</b></small><br>";
}
return TRUE;
}
// here we need to check, whether day is a saturday
//elseif($arrTargetDate['wday']==6 || $arrTargetDate['wday']==0) {
// if($debug) {
// echo "<small><b>".date("d.m.y",$thisDay)." is on a weekend!</b></small><br>";
// }
// return TRUE;
// }
return FALSE;
}
function workingdays($startdate, $enddate){
$startdate = explode("/", $startdate); //format dd/mm/YYYY
$enddate = explode("/", $enddate); //format dd/mm/YYYY
$starttime = mktime('0', '0', '0', $startdate[1], $startdate[0], $startdate[2]);
$endtime = mktime('0', '0', '0', $enddate[1], $enddate[0], $enddate[2]);
$noofdays = ceil(($endtime - $starttime) / (60 * 60 * 24)) + 1;
$sundaycounter = 0;
$saturdaycounter = 0;
$weekdaycounter = 0;
$holidaycounter = 0;
echo "No. of days = ".$noofdays."<br>";
for ($i = 0; $i < $noofdays; $i++){
$ts = mktime('0', '0', '0', $startdate[1], ($startdate[0] + $i), $startdate[2]);
$test=CheckHolidays($ts);
if($test == TRUE) {
$holidaycounter++;
} else {
if (date("l", $ts) == "Sunday"){
$sundaycounter ++;
} elseif (date("l", $ts) == "Saturday"){
$saturdaycounter ++;
} else {
$weekdaycounter ++;
}
}
}
echo "stats are<br>~~~~~~~~~<br>Sundays = ".$sundaycounter."<br>Saturdays = ".$saturdaycounter."<br> Weekdays = ".$weekdaycounter."<br> Bank holidays = ".$holidaycounter;
}
$startdate = "01/01/2007";
$enddate = "12/01/2008";
workingdays($startdate, $enddate);
$benchtime_e = microtime_float();
$time = $benchtime_e - $benchtime_s;
echo "<br><br>Ran in $time seconds\n";

LinkBack URL
About LinkBacks













Bookmarks