亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以 PHP 計算工作時間時差

以 PHP 計算工作時間時差

PHP
陪伴而非守候 2022-09-17 17:57:01
我想計算編碼日期和完成日期之間的小時數。截至目前,我可以計算兩個日期時間之間的時差,不包括星期六和星期日。我的問題是我需要排除午休時間(上午12:00至下午1:00)和假期。這是我的代碼:<?phpfunction business_hours($start, $end){    $startDate = new DateTime($start);    $endDate = new DateTime($end);    $periodInterval = new DateInterval( "PT1H" );    $period = new DatePeriod( $startDate, $periodInterval, $endDate );    $count = 0;    $holidays = [      "Human Rights Day"      => new DateTime(date('Y') . '-03-21'),      "Good Friday"           => new DateTime(date('Y') . '-03-30'),      "Family Day"            => new DateTime(date('Y') . '-04-02'),      "Freedom Day"           => new DateTime(date('Y') . '-04-27'),      "Labour Day"            => new DateTime(date('Y') . '-05-01'),      "Youth Day"             => new DateTime(date('Y') . '-06-16'),      "National Women's Day"  => new DateTime(date('Y') . '-08-09'),      "Heritage Day"          => new DateTime(date('Y') . '-09-24'),      "Day of Reconciliation" => new DateTime(date('Y') . '-12-16'),    ];    foreach($period as $date){    $startofday = clone $date;    $startofday->setTime(8,00);    $endofday = clone $date;    $endofday->setTime(17,00);        if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday'))){            $count++;        }    }    echo $count;}$start = '2020-02-14 08:00:00';$end = '2020-02-17 08:00:00';$go = business_hours($start,$end);//output 9 hours?>根據我的代碼,我將工作時間設置為8:00至17:00,然后排除了周六和周日。根據我的例子,輸出將為9(包括午休時間)。如何排除午休和節假日?更新完成時間可以超過 1 天。例如,開始日期/時間為 2020-02-14 08:00:00,完成時間為 2020-02-19 08:00:00,輸出為 27 小時。計算應排除每天中午12:00至下午1:00的午休時間以及介于兩者之間的假期。更新我添加了一系列假期,如何排除這些假期?
查看完整描述

2 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

由于您使用的是靜態/定義的午休時間,因此可以使用以下邏輯:

  • 如果$startDate是工作日,并且在12:00之前,則為午餐計算的開始日期;否則就是下一個工作日。

  • 如果$endDate是工作日,13:00之后,則為午餐計算的結束日期;否則,是前一個工作日

  • 實際午休時間數等于這些重新計算的日期之間的差額,每個日期一小時。以下是偽代碼:

   if(!in_array($startDate->format('l'), array('Sunday','Saturday')) && ($startDate->format('g') < 12) {

        $startForLunch = $startDate;

    }

    else {

        $startForLunch = new DateTime($start, '+1 day');

        while(in_array($startDate->format('l'), array('Sunday','Saturday'))){

            $startForLunch->add('1 day');

        }

    }


    if(!in_array($endDate->format('l'), array('Sunday','Saturday')) && ($endDate->format('g') > 13) {

        $endForLunch = $end;

    }

    else {

        $endForLunch = new DateTime($endDate, '-1 day');

        while(in_array($endDate->format('l'), array('Sunday','Saturday'))){

            $endForLunch->add('-1 day');

        }

    }

    $lunchPeriods = $endForLunch - $startForLunch + 1;


查看完整回答
反對 回復 2022-09-17
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

嘗試每天在循環中添加一些條件以跳過幾個小時怎么樣?


($date->format('H') <= 12 || $date->format('H') > 13)

如果小時高于12到13,它將跳過(如果您使用24格式小時)


if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday')) && ($date->format('H') <= 12 || $date->format('H') > 13)){


   $count++;


}

更新


好吧,如果你的“圣日”是cutom,也許最好在你的功能中添加一些參數來存儲你的圣日列表


function business_hours($start, $end,$holyDays = null){

  //another line

}

并檢查每個日期循環是否在圣日列表中


in_array($date->format('Y-m-d'), $holyDays)

您將獲得休息時間和自定義圣日的過濾器


function business_hours($start, $end,$holyDays = null){


    $startDate = new DateTime($start);

    $endDate = new DateTime($end);

    $periodInterval = new DateInterval( "PT1H" );


    $period = new DatePeriod( $startDate, $periodInterval, $endDate );

    $count = 0;




    foreach($period as $date){

        $startofday = clone $date;

        $startofday->setTime(8,00);

        $endofday = clone $date;

        $endofday->setTime(17,00);


        //just some var to use in conditional check

        $notHolyday = true;


        //now check the array of holyday and check if in range dates is same with holydays we have

        if($holyDays != null && is_array($holyDays) && in_array($date->format('Y-m-d'), $holyDays)){

            $notHolyday = false;

        }


        if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday')) && ($date->format('H') <= 12 || $date->format('H') > 13) && $notHolyday){

            $count++;


        }


    }

    echo $count;

}


$start = '2020-02-14 08:00:00';

$end = '2020-02-19 08:00:00';

$holyDays = array('2020-02-17','2020-02-18');

$count = business_hours($start,$end,$holyDays);


echo $count;

我希望這是你想要的


查看完整回答
反對 回復 2022-09-17
  • 2 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號