猿问

计算营业天数

计算营业天数

我需要一个在PHP中添加“业务天数”的方法。例如,周五12/5+3个工作日=周三12/10。

至少,我需要这些代码来理解周末,但理想情况下,它也应该考虑到美国联邦假日。如果有必要的话,我相信我可以用暴力来解决问题,但我希望有一种更优雅的方法。有人吗?

谢谢。


一只名叫tom的猫
浏览 871回答 3
3回答

森林海

这里有一个函数来自用户评论在PHP手册中的Date()函数页上。这是对注释中早期函数的改进,增加了对闰年的支持。输入开始日期和结束日期,以及可能介于两者之间的任何假日的数组,然后将工作日作为整数返回:<?php//The&nbsp;function&nbsp;returns&nbsp;the&nbsp;no.&nbsp;of&nbsp;business&nbsp;days&nbsp;between&nbsp;two&nbsp;dates&nbsp;and&nbsp;it&nbsp;skips&nbsp;the&nbsp;holidaysfunction&nbsp; getWorkingDays($startDate,$endDate,$holidays){ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;strtotime&nbsp;calculations&nbsp;just&nbsp;once &nbsp;&nbsp;&nbsp;&nbsp;$endDate&nbsp;=&nbsp;strtotime($endDate); &nbsp;&nbsp;&nbsp;&nbsp;$startDate&nbsp;=&nbsp;strtotime($startDate); &nbsp;&nbsp;&nbsp;&nbsp;//The&nbsp;total&nbsp;number&nbsp;of&nbsp;days&nbsp;between&nbsp;the&nbsp;two&nbsp;dates.&nbsp;We&nbsp;compute&nbsp;the&nbsp;no.&nbsp;of&nbsp;seconds&nbsp;and&nbsp;divide&nbsp;it&nbsp;to&nbsp;60*60*24 &nbsp;&nbsp;&nbsp;&nbsp;//We&nbsp;add&nbsp;one&nbsp;to&nbsp;inlude&nbsp;both&nbsp;dates&nbsp;in&nbsp;the&nbsp;interval. &nbsp;&nbsp;&nbsp;&nbsp;$days&nbsp;=&nbsp;($endDate&nbsp;-&nbsp;$startDate)&nbsp;/&nbsp;86400&nbsp;+&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;$no_full_weeks&nbsp;=&nbsp;floor($days&nbsp;/&nbsp;7); &nbsp;&nbsp;&nbsp;&nbsp;$no_remaining_days&nbsp;=&nbsp;fmod($days,&nbsp;7); &nbsp;&nbsp;&nbsp;&nbsp;//It&nbsp;will&nbsp;return&nbsp;1&nbsp;if&nbsp;it's&nbsp;Monday,..&nbsp;,7&nbsp;for&nbsp;Sunday &nbsp;&nbsp;&nbsp;&nbsp;$the_first_day_of_week&nbsp;=&nbsp;date("N",&nbsp;$startDate); &nbsp;&nbsp;&nbsp;&nbsp;$the_last_day_of_week&nbsp;=&nbsp;date("N",&nbsp;$endDate); &nbsp;&nbsp;&nbsp;&nbsp;//---->The&nbsp;two&nbsp;can&nbsp;be&nbsp;equal&nbsp;in&nbsp;leap&nbsp;years&nbsp;when&nbsp;february&nbsp;has&nbsp;29&nbsp;days,&nbsp;the&nbsp;equal&nbsp;sign&nbsp;is&nbsp;added&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;//In&nbsp;the&nbsp;first&nbsp;case&nbsp;the&nbsp;whole&nbsp;interval&nbsp;is&nbsp;within&nbsp;a&nbsp;week,&nbsp;in&nbsp;the&nbsp;second&nbsp;case&nbsp;the&nbsp;interval&nbsp;falls&nbsp;in&nbsp;two&nbsp;weeks. &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($the_first_day_of_week&nbsp;<=&nbsp;$the_last_day_of_week)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($the_first_day_of_week&nbsp;<=&nbsp;6&nbsp;&&&nbsp;6&nbsp;<=&nbsp;$the_last_day_of_week)&nbsp;$no_remaining_days--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($the_first_day_of_week&nbsp;<=&nbsp;7&nbsp;&&&nbsp;7&nbsp;<=&nbsp;$the_last_day_of_week)&nbsp;$no_remaining_days--; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;(edit&nbsp;by&nbsp;Tokes&nbsp;to&nbsp;fix&nbsp;an&nbsp;edge&nbsp;case&nbsp;where&nbsp;the&nbsp;start&nbsp;day&nbsp;was&nbsp;a&nbsp;Sunday &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;the&nbsp;end&nbsp;day&nbsp;was&nbsp;NOT&nbsp;a&nbsp;Saturday) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;the&nbsp;day&nbsp;of&nbsp;the&nbsp;week&nbsp;for&nbsp;start&nbsp;is&nbsp;later&nbsp;than&nbsp;the&nbsp;day&nbsp;of&nbsp;the&nbsp;week&nbsp;for&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($the_first_day_of_week&nbsp;==&nbsp;7)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;if&nbsp;the&nbsp;start&nbsp;date&nbsp;is&nbsp;a&nbsp;Sunday,&nbsp;then&nbsp;we&nbsp;definitely&nbsp;subtract&nbsp;1&nbsp;day &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$no_remaining_days--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($the_last_day_of_week&nbsp;==&nbsp;6)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;if&nbsp;the&nbsp;end&nbsp;date&nbsp;is&nbsp;a&nbsp;Saturday,&nbsp;then&nbsp;we&nbsp;subtract&nbsp;another&nbsp;day &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$no_remaining_days--; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;the&nbsp;start&nbsp;date&nbsp;was&nbsp;a&nbsp;Saturday&nbsp;(or&nbsp;earlier),&nbsp;and&nbsp;the&nbsp;end&nbsp;date&nbsp;was&nbsp;(Mon..Fri) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;so&nbsp;we&nbsp;skip&nbsp;an&nbsp;entire&nbsp;weekend&nbsp;and&nbsp;subtract&nbsp;2&nbsp;days &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$no_remaining_days&nbsp;-=&nbsp;2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//The&nbsp;no.&nbsp;of&nbsp;business&nbsp;days&nbsp;is:&nbsp;(number&nbsp;of&nbsp;weeks&nbsp;between&nbsp;the&nbsp;two&nbsp;dates)&nbsp;*&nbsp;(5&nbsp;working&nbsp;days)&nbsp;+&nbsp;the&nbsp;remainder &nbsp;&nbsp;&nbsp;&nbsp;//---->february&nbsp;in&nbsp;none&nbsp;leap&nbsp;years&nbsp;gave&nbsp;a&nbsp;remainder&nbsp;of&nbsp;0&nbsp;but&nbsp;still&nbsp;calculated&nbsp;weekends&nbsp;between&nbsp;first&nbsp;and&nbsp;last&nbsp;day,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;this&nbsp;is&nbsp;one&nbsp;way&nbsp;to&nbsp;fix&nbsp;it &nbsp;&nbsp;&nbsp;$workingDays&nbsp;=&nbsp;$no_full_weeks&nbsp;*&nbsp;5; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($no_remaining_days&nbsp;>&nbsp;0&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$workingDays&nbsp;+=&nbsp;$no_remaining_days; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//We&nbsp;subtract&nbsp;the&nbsp;holidays &nbsp;&nbsp;&nbsp;&nbsp;foreach($holidays&nbsp;as&nbsp;$holiday){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$time_stamp=strtotime($holiday); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//If&nbsp;the&nbsp;holiday&nbsp;doesn't&nbsp;fall&nbsp;in&nbsp;weekend &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($startDate&nbsp;<=&nbsp;$time_stamp&nbsp;&&&nbsp;$time_stamp&nbsp;<=&nbsp;$endDate&nbsp;&&&nbsp;date("N",$time_stamp)&nbsp;!=&nbsp;6&nbsp;&&&nbsp;date("N",$time_stamp)&nbsp;!=&nbsp;7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$workingDays--; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$workingDays;}//Example:$holidays=array("2008-12-25","2008-12-26","2009-01-01"); &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;getWorkingDays("2008-12-22","2009-01-02",$holidays)//&nbsp;=>&nbsp;will&nbsp;return&nbsp;7?>

手掌心

得到.的号码没有假期的工作日在两个日期之间:使用示例:echo&nbsp;number_of_working_days('2013-12-23',&nbsp;'2013-12-29');产出:3职能:function&nbsp;number_of_working_days($from,&nbsp;$to)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$workingDays&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5];&nbsp;#&nbsp;date&nbsp;format&nbsp;=&nbsp;N&nbsp;(1&nbsp;=&nbsp;Monday,&nbsp;...) &nbsp;&nbsp;&nbsp;&nbsp;$holidayDays&nbsp;=&nbsp;['*-12-25',&nbsp;'*-01-01',&nbsp;'2013-12-23'];&nbsp;#&nbsp;variable&nbsp;and&nbsp;fixed&nbsp;holidays &nbsp;&nbsp;&nbsp;&nbsp;$from&nbsp;=&nbsp;new&nbsp;DateTime($from); &nbsp;&nbsp;&nbsp;&nbsp;$to&nbsp;=&nbsp;new&nbsp;DateTime($to); &nbsp;&nbsp;&nbsp;&nbsp;$to->modify('+1&nbsp;day'); &nbsp;&nbsp;&nbsp;&nbsp;$interval&nbsp;=&nbsp;new&nbsp;DateInterval('P1D'); &nbsp;&nbsp;&nbsp;&nbsp;$periods&nbsp;=&nbsp;new&nbsp;DatePeriod($from,&nbsp;$interval,&nbsp;$to); &nbsp;&nbsp;&nbsp;&nbsp;$days&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;($periods&nbsp;as&nbsp;$period)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!in_array($period->format('N'),&nbsp;$workingDays))&nbsp;continue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(in_array($period->format('Y-m-d'),&nbsp;$holidayDays))&nbsp;continue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(in_array($period->format('*-m-d'),&nbsp;$holidayDays))&nbsp;continue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$days++; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$days;}

慕斯709654

有一些ARG为日期()应该有帮助的功能。如果你检查日期(“w”),它将给你一周中的一天的数字,从星期天的0到星期六的6。所以.。也许就像.。$busDays&nbsp;=&nbsp;3;$day&nbsp;=&nbsp;date("w");if(&nbsp;$day&nbsp;>&nbsp;2&nbsp;&&&nbsp;$day&nbsp;<=&nbsp;5&nbsp;)&nbsp;{&nbsp;/*&nbsp;if&nbsp;between&nbsp;Wed&nbsp;and&nbsp;Fri&nbsp;*/ &nbsp;&nbsp;$day&nbsp;+=&nbsp;2;&nbsp;/*&nbsp;add&nbsp;2&nbsp;more&nbsp;days&nbsp;for&nbsp;weekend&nbsp;*/}$day&nbsp;+=&nbsp;$busDays;这只是一个粗略的例子。
随时随地看视频慕课网APP
我要回答