Home > Article > Backend Development > Calculate the date after a specified working day_PHP Tutorial
In a recent project, I need to use such a function to calculate the date after N working days. Now I think of the following solution and it seems that it can solve the problem.
Under this explanation, working days are still calculated according to national regulations, that is, Monday to Friday, and those who need to work before and after statutory holidays are counted as working days, while Saturdays, Sundays and statutory holidays are not counted as work. Days, statutory holidays and days off work can be stored in the database for maintenance.
The code is as follows. I feel it is not very efficient. If there is a better way, please share it with experts.
<?<span php date_default_timezone_set(</span>"Asia/Shanghai");<span //</span><span 设置好一个时区</span> <span $now</span>=<span mktime</span>(0, 0, 0,8,1,2013);<span //</span><span 开始的日期,可以是现在或其他你想要的某一个日期的时间戳,这里设置2013-08-01</span> <span $i</span>=10;<span //</span><span 要求计数的工作日数量</span> <span /*</span><span 国家法定假日一般可以存储在数据库中进行维护,包括休假的日期,和周末调休上班的日期两类,作为演示对应日期暂时放在数组中</span><span */</span> <span $working</span>=<span array</span>(<span mktime</span>(0, 0, 0,8,3,2013),<span mktime</span>(0, 0, 0,8,11,2013));<span //</span><span 周末调休上班的日期</span> <span $holiday</span>=<span array</span>(<span mktime</span>(0, 0, 0,8,8,2013),<span mktime</span>(0, 0, 0,8,6,2013));<span //</span><span 法定假期</span> <span $day</span>=<span date</span>("d",<span $now</span>);<span //</span><span 把当天的日期给一个变量</span> <span $w</span>=0;<span //</span><span 工作日计数变量</span> <span while</span>(<span $w</span><<span $i</span>){<span //</span><span 当达到要求的工作日数量则停止循环</span> <span $newdate</span>=<span mktime</span>(0, 0, 0,<span date</span>("m",<span $now</span>),++<span $day</span>,<span date</span>("Y",<span $now</span>));<span //</span><span 加一天输出新的时间戳</span> <span if</span>(<span in_array</span>(<span $newdate</span>, <span $working</span>)){<span //</span><span 判断一下,如果属于调休的则工作日计数加一,如果法定假日调休存在数据库中,则这里需要一条查询语句来查询一下</span> <span $w</span>++<span ; }</span><span else</span>{<span //</span><span 如不属于调休的则再进行判断,既不是周六周日,也不是法定假日,则工作日计数再加上一</span> <span if</span>(<span date</span>("w",<span $newdate</span>)!=0&&<span date</span>("w",<span $newdate</span>)!=6&&!<span in_array</span>(<span $newdate</span>, <span $holiday</span><span )){ </span><span $w</span>++<span ; } } } </span><span echo</span> <span date</span>("Y-m-d",<span $newdate</span>);<span //</span><span 最后得出的时间戳就是要求的10个工作日后的日期了。</span> ?>