|
|
|
| Title : |
Calculate a date from a given date with given days but omit weekends and holidays, which can be provided either as manual array or from database. Important for anyone, who needs to calculate based on workdays. |
| Categories : |
Date Time, PHP |
 Andy Krause |
| Date : |
Jun 07th 2001 |
| Grade : |
5 of 5 (graded 5 times) |
| Viewed : |
13462 |
| File : |
datecalc.php
|
| Images : |
No Images for this code example. |
|
| Search : |
More code by Andy Krause |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?
/*
As I didn't find any solution on the web for date calculations with consideration of holidays
and weekends, I wrote my own utility for that in PHP. It can be combined with mySQL
Database for holidays etc.
Please get the attached file as a demonstration of how it basically works.
The first part displays a form to enter start-date, no. of days and if we should consider
weekends and holidays or not.
After submitting the form, we get a result with optional comprehensive output.
Explanation in brief:
This example is based on two major functions:
*/
function CheckHolidays($thisDay) {
global $debug;
// holidays are an array either manually set, included from file or from
database
$holiday[0]="2001-06-04";
$holiday[1]="2001-06-14";
$holiday[2]="2001-12-25";
$holiday[3]="2001-12-26";
$holiday[4]="2002-01-01";
$arrTargetDate=getdate($thisDay);
// Calculate Public Holidays
for($x=0; $x<count($holiday); $x++) {
$ftdate=ConvertMysqlDate($holiday[$x]);
if($thisDay==$ftdate) {
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
if($arrTargetDate['wday']==6) {
if($debug) {
echo "<small><b>".date
("d.m.y",$thisDay)." is a Saturday!</b></small><br>";
}
return TRUE;
}
// here we need to check, whether day is a sunday
elseif($arrTargetDate['wday']==0) {
if($debug) {
echo "<small><b>".date
("d.m.y",$thisDay)." is a Sunday!</b></small><br>";
}
return TRUE;
}
return FALSE;
}
// this is the core calculation function
function CalculateEndDate($startDate,$days) {
global $counter,$intUnixTargetDate,$intSpecialDays,$debug;
$counter=0;
for($x=1; $x<=$days; $x++) {
$intUnixTargetDate = $startDate;
$intUnixTargetDate = $intUnixTargetDate+($x*86400);
$test=FALSE;
$test=CheckHolidays($intUnixTargetDate);
if($test == TRUE) {
$counter++;
}
}
if($counter != 0) {
$intSpecialDays += $counter;
if($debug) {
echo "<br><small><strong>$counter special days!
</strong>";
echo " This is ".date
("d.m.y",$intUnixTargetDate)." plus ".$counter." days</small><br><br>";
}
CalculateEndDate($intUnixTargetDate,$counter);
}
elseif($counter == 0) {
return TRUE;
}
return FALSE;
}
?>
Please let me know, if there are any bugs or improvements to be done.
Enjoy
ndee
|
|
| A Timing Class Categories : PHP, PHP Classes, Date Time | | | UTC time detect in php Categories : PHP, Date Time | | | Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time | | | A wrapper function to format dates coming from a databases with the
same syntax as PHP's date() function. Categories : Date Time, Databases, PHP | | | pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes | | | A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function. Categories : PHP, PHP Classes, Calendar, Date Time | | | PHP Round Clock - Must have Gif support to use this. Categories : PHP, Date Time, Graphics | | | A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed. Categories : Date Time, PHP, Complete Programs, Calendar | | | Open and Close your website in fixed times . Categories : PHP, PHP Classes, Cron, Date Time | | | mysql date/time converters Categories : PHP, MySQL, Databases, Date Time | | | Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used. Categories : HTML and PHP, PHP, Date Time | | | If you want to create select buttons featuring current date this example will show you how... Categories : Date Time, HTML and PHP, PHP | | | Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example. Categories : PHP, Date Time, PHP Classes, Calendar | | | calculus of the eastersunday Categories : BC math, PHP, Date Time, Databases | | | Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June). Categories : Calendar, Date Time, PHP | |
|
|
|