Last Update:2017-08-29
Source: Internet
Author: User
Keywords
Web Programming
PHP Tutorials
PHP tutorial excludes weekends and Holidays program instance code date_default_timezone_set (' PRC '); /** * The date after a certain number of days from a date, * excludes Saturday Sundays and holidays * @param $start start date * @param $offset after days * @param $exception exceptional Holiday * @param $allow allowed dates (reservation parameters) * @return * Examples: input (2010-06-25, 5, '), get 2010-07-02 */ function getendday ($start = ' Now ', $offset =0, $exception = ', $allow = ') { ///First calculation does not exclude the results of the Saturday Sundays and holidays $starttime = Strtotime ($start); $endtime = $starttime + $offset * 24 * 3600; $end = Date (' y-m-d ', $endtime); //Then calculates the offset caused by Sunday Saturday $weekday = Date (' n ', $starttime);//Get Week value: 1-7 $remain = $offset% 7; $newoffset = 2 * ($offset-$remain)/7;//to be recalculated for two days every week if ($remain > 0) {//Zhou Yu rounding $tmp = $weekday + $remain; if ($tmp >= 7) { $newoffset + 2; }else if ($tmp = = 6) { $newoffset + 1; } //Consideration of current situation for Saturday Sunday if ($weekday = = 6) { $newoffset-= 1; }else if ($weekday = = 7) { $newoffset-= 2; } } //Recalculate holiday-induced offsets if (Is_array ($exception)) {//Multiple holidays foreach ($exception as $day) { $tmp _time = Strtotime ($day); if ($tmp _time> $starttime && $tmp _time<= $endtime) {//within range (A,B) $weekday _t = Date (' n ', $tmp _time); if ($weekday _t <= 5) {//Prevent recurring holidays and weekends $newoffset + 1; } } } }else{//Single Holiday if ($exception!= ') { $tmp _time = Strtotime ($exception); if ($tmp _time> $starttime && $tmp _time<= $endtime) { $weekday _t = Date (' n ', $tmp _time); if ($weekday _t <= 5) { $newoffset + 1; } } } } ///based on the number of offset days, recursive equivalent operations jzread.com if ($newoffset > 0) { #echo ' [{$start}-> {$offset}] = [{$end}-> {$newoffset}] '. <br/>n "; return Getendday ($end, $newoffset, $exception, $allow); }else{ return $end; } } /** * Violence Cycle method */ function getendday2 ($start = ' Now ', $offset =0, $exception = ', $allow = ') { $starttime = Strtotime ($start); $tmptime = $starttime + 24*3600; while ($offset > 0) { $weekday = Date (' n ', $tmptime); $tmpday = Date (' y-m-d ', $tmptime); $BFD = false;//whether holiday if (Is_array ($exception)) { $BFD = In_array ($tmpday, $exception); }else{ $BFD = ($exception = = $tmpday); } if ($weekday <=5 &&! $bfd) {//not weekends and holidays $offset-; #echo "tmpday={$tmpday}". <br/> "; } $tmptime + + 24*3600; } return $tmpday; } $exception = Array ( ' 2010-01-01 ', ' 2010-01-02 ', ' 2010-01-03 ', ' 2010-04-03 ', ' 2010-04-04 ', ' 2010-04-05 ', ' 2010-05-01 ', ' 2010-05-02 ', ' 2010-05-03 ', ' 2010-06-14 ', ' 2010-06-15 ', ' 2010-06-16 ', ' 2010-09-22 ', ' 2010-09-23 ', ' 2010-09-24 ', ' 2010-10-01 ', ' 2010-10-02 ', ' 2010-10-03 ', ' 2010-10-04 ', ' 2010-10-05 ', ' 2010-10-06 ', ' 2010-10-07 ', ); //echo getendday (' 2010-08-27 ', 3, '); //echo getendday (' 2010-06-25 ', 15, ' 2010-07-07 '); $t 1 = microtime (); echo getendday (' 2010-05-12 ', A, $exception). " <br/> "; $t 2 = Microtime (); echo "Use". ($t 2-$t 1). " s <br/> "; echo getendday2 (' 2010-05-12 ', A, $exception). " <br/> "; $t 3 = Microtime (); echo "Use". ($t 3-$t 2). " s <br/> ";