dtmu88600 2015-11-05 11:07
浏览 51
已采纳

如何通过在PHP中给出日期来识别偶数或星期六

I want to calculate total number of working days in between two date. Here we include second and fourth Saturday as working day(i.e all Even Saturdays are considered as Holiday) I can get the day of the particular date by using below code

$day = ‘2015-11-07’;
$dayName = date("l",strtotime($day));

if ($dayName =='saturday') {
    ...
}

then I have to find whether that particular date falls under first Saturday of November or second Saturday. Is there any option in doing that?

I have the code to calculate total number of days excluding all Saturdays,Sunday and holidays. I got that code from another question

But I want to identify even saturdays and I will detect that saturdays in total days.

  • 写回答

2条回答 默认 最新

  • dongxie3681 2015-11-05 14:27
    关注

    Get all days between the two end-points, then apply your logic to each, accumulating the ones that match:

    function work_days_between_two_dates(\DateTime $begin, \DateTime $end) {
        $workdays = [];
        $all_days = new DatePeriod($begin, new DateInterval('P1D'), $end->modify('+1 day'));
        foreach ($all_days as $day) {
            $dow = (int)$day->format('w');
            $dom = (int)$day->format('j');
            if (1 <= $dow && $dow <= 5) { // Mon - Fri
                $workdays[] = $day;
            } else if (6 == $dow && 0 == $dom % 2) { // Even Saturday
                $workdays[] = $day;
            }
        }
        return $workdays;
    }
    

    Live example.

    For November 2015 (dates between 11/1 and 12/1), this algorithm marks the following as work days:

        November 2015
    Su Mo Tu We Th Fr Sa
     -  2  3  4  5  6  -
     -  9 10 11 12 13 14
     - 16 17 18 19 20 -
     - 23 24 25 26 27 28
     - 30
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大