WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 Update Picture
Andy Krause
Date : Jun 07th 2001
Grade : 5 of 5 (graded 6 times)
Viewed : 15887
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

Submit your own code examples 
 

<?

/*
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




Function to convert Arabic numbers into Roman Numerals
Categories : Algorithms, PHP, Date Time
Customizable Calendar Class
Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
function to generate calendars on the fly.
Categories : Calendar, PHP, Date Time
Change the background color of a website daily dynamically using the php date function to get the current day of the week and depending on that day, set the background color for the web page.
Categories : PHP, Date Time, Beginner Guides, Web Design
Phorum, MySQL, Language, UK date format, MySQL UK Date format
Categories : PHP, Date Time, Strings, MySQL, Databases
PHP Calendar Web App
Categories : PHP, Databases, MySQL, Date Time, 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
Monthly and Daily Upcoming Events calendar.
Categories : Date Time, PostgreSQL, PHP, Calendar, Databases
Website colour changer
Categories : PHP, HTML and PHP, Date Time
PHP Calendar
Categories : PHP, Calendar, Date Time, Java Script, CSS
PHP Round Clock - Must have Gif support to use this.
Categories : PHP, Date Time, Graphics
This script contains 2 functions: 1 to create html select object based on your own customer date format entry- "M d Y h:i.... etc". The second function processes the select object on submit back to unix time.
Categories : PHP, Calendar, Date Time, HTML and PHP
Select with current month
Categories : PHP, HTML and PHP, Date Time, Arrays