Home >Backend Development >PHP Tutorial >php date aspect
date_default_timezone_set('PRC');
/**
* Get the date after a certain number of days have passed since a certain date,
* Exclude Saturdays, Sundays and holidays
* @param $start Start date
* @param $offset Number of days passed
* @param $exception Exception holidays
* @param $allow Allowed dates (reserved parameters)
* @return
* examples: input (2010-06-25,5,''), get 2010-07-02
*/
function getEndDay( $start='now', $offset=0, $exception='', $allow='' ){
//First The calculation does not exclude the results of Saturdays, Sundays and holidays
$starttime = strtotime($start);
$endtime = $starttime + $offset * 24 * 3600;
$end = date('Y-m-d', $endtime) ;
//Then calculate the offset caused by Saturday and Sunday
$weekday = date('N', $starttime); // Get the weekday value: 1-7
$remain = $offset % 7;
$newoffset = 3 $tmp >= 7 ){
if ( $weekday == 6 ){
offset
if ( is_array($exception) ){//Multiple holidays
//Within the range (a,b]
use using using using through using ’ ’s ’ through ’ through using using using using off out off out out out out out out out out out out out out out Out out out of when to 5------, ;
}
_time>$starttime && $tmp_time<=$endtime ) {
$weekday_t Do equivalent operations recursively
if($newoffset > 0){
#echo "[{$start} -> {$offset}] = [{$end} -> {$newoffset}]"."< ; br /& gt; n "; return $end;
}
}
/**
* Violent 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;//是否节假日
if(is_array($exception)){
$bfd = in_array($tmpday,$exception);
}else{
$bfd = ($exception==$tmpday);
}
if( $weekday<=5 && !$bfd){//不是周末和节假日
$offset--;
#echo "tmpday={$tmpday}"."
";
}
$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');
$t1 = microtime();
echo getEndDay('2010-05-12',66,$exception)."
";
$t2 = microtime();echo "use ".($t2-$t1)." s
";
echo getEndDay2('2010-05-12',66,$exception)."
";
$t3 = microtime();echo "use ".($t3-$t2)." s
";