200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > php-获取从今天开始计算第5个工作日的日期

php-获取从今天开始计算第5个工作日的日期

时间:2024-03-10 04:19:59

相关推荐

php-获取从今天开始计算第5个工作日的日期

<?php

/**

* @author: hirry

* @email: ljhynlp@

* @time: -06-05

* @words: 获取从今天开始退回的第n个工作日的日期

*/

//定义当前时间

define("TODAY",date("Y-m-d"));

//定义要获取第几个工作日的日期

define("NUMDAY",'5');

//计算出第5天的时间

$fifthday = get_num_date(TODAY, NUMDAY);

//获取第5个工作日

echo "第5个工作日期是:";

echo get_fifth_work_date(TODAY, $fifthday);

//计算第n天的日期

function get_num_date($startDate, $numDate){

return date("Y-m-d", strtotime($startDate) + $numDate*3600*24);

}

//计算第5个工作日期

function get_fifth_work_date($toDay, $endday){

$num = check_work_day_num($toDay, $endday);

if($num==5){

echo $endday;

}else{

$endday2 = get_num_date(TODAY, NUMDAY+(NUMDAY-$num));

get_fifth_work_date($toDay, $endday2);

}

}

//计算工作日函数

function check_work_day_num($toDay, $endday){

$starttime = $toDay;

$endtime = $endday;

$starttime=date("z",mktime(0,0,0,substr($starttime,5,2),substr($starttime,8,2),substr($starttime,0,4))); $endtime=date("z",mktime(0,0,0,substr($endtime,5,2),substr($endtime,8,2),substr($endtime,0,4)));

$alltime=$endtime-$starttime+1;

//节假日的日期数据,目前按照年份配置就可以了

$ordainrest[""] = "05-01&3|&&|06-10&3|&&|10-01&5";

$ordainrest[""] = "05-01&3|&&|06-10&3|&&|10-01&5";

//计算去掉节假日的天数

$tmpa=explode("|&&|",$ordainrest[date("Y")]);

for($Tmpb=0;$Tmpb<count($tmpa);$Tmpb++){

$tmpc=explode("&",$tmpa[$Tmpb]);

$tmpsize=date("z",mktime(0,0,0,substr($tmpc[0],0,2),substr($tmpc[0],3,2),date("Y")));

if($tmpsize>=$starttime && $tmpsize<=$endtime){

$alltime=$alltime-$tmpc[1];

//从总的工作日中去掉法定日期

}

}

//计算去掉周末的天数

$weekdaynum = get_weekend_days($toDay, $endday);

$work_day_num = $alltime-($weekdaynum-1);

return $work_day_num;

}

//获取周末天数

function get_weekend_days($start_date,$end_date,$is_workday = false){

if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date);

$start_reduce = $end_add = 0;

$start_N = date('N',strtotime($start_date));

$start_reduce = ($start_N == 7) ? 1 : 0;

$end_N = date('N',strtotime($end_date));

in_array($end_N,array(6,7)) & $end_add = ($end_N == 7) ? 2 : 1;

$alldays = abs(strtotime($end_date) - strtotime($start_date))/86400 + 1;

$weekend_days = floor(($alldays + $start_N - 1 - $end_N) / 7) * 2 - $start_reduce + $end_add;

if ($is_workday){

$workday_days = $alldays - $weekend_days;

return $workday_days;

}

return $weekend_days;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。