久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

計算PHP中兩個日期之間的工作時間

calc the working hours between 2 dates in PHP(計算PHP中兩個日期之間的工作時間)
本文介紹了計算PHP中兩個日期之間的工作時間的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個函數(shù)可以返回兩個日期之間的差異,但是我需要計算出工作時間的差異,假設是周一到周五(上午 9 點到下午 5:30):

I have a function to return the difference between 2 dates, however I need to work out the difference in working hours, assuming Monday to Friday (9am to 5:30pm):

//DATE DIFF FUNCTION
// Set timezone
date_default_timezone_set("GMT");

// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
        $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
        $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
        $ttime = $time1;
        $time1 = $time2;
        $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
        // Set default diff to 0
        $diffs[$interval] = 0;
        // Create temp time from time1 and interval
        $ttime = strtotime("+1 " . $interval, $time1);
        // Loop until temp time is smaller than time2
        while ($time2 >= $ttime) {
            $time1 = $ttime;
            $diffs[$interval]++;
            // Create new temp time from time1 and interval
            $ttime = strtotime("+1 " . $interval, $time1);
        }
    }

    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
        // Break if we have needed precission
        if ($count >= $precision) {
            break;
        }
        // Add value and interval 
        // if value is bigger than 0
        if ($value > 0) {
            // Add s if value is not 1
            if ($value != 1) {
                $interval .= "s";
            }
            // Add value and interval to times array
            $times[] = $value . " " . $interval;
            $count++;
        }
    }

    // Return string with times
    return implode(", ", $times);
}

日期 1 = 2012-03-24 03:58:58
日期 2 = 2012-03-22 11:29:16

是否有一種簡單的方法可以做到這一點,即 - 計算一周工作時間的百分比并使用上述函數(shù)除以差異 - 我已經(jīng)嘗試過這個想法并得到了一些非常奇怪的數(shù)字......

Is there a simple way of doing this, i.e - calculating the percentage of working hours in a week and dividing the difference using the above function - I have played around with this idea and got some very strange figures...

或者有更好的方法......?

Or is there better way....?

推薦答案

這個例子使用 PHP 的內(nèi)置 DateTime 類來進行日期計算.我的處理方法是首先計算兩個日期之間的完整工作日數(shù),然后將其乘以 8(見注釋).然后它獲取部分日期的工作時間并將它們添加到總工作時間中.把它變成一個函數(shù)會很簡單.

This example uses PHP's built in DateTime classes to do the date math. How I approached this was to start by counting the number of full working days between the two dates and then multiply that by 8 (see notes). Then it gets the hours worked on the partial days and adds them to the total hours worked. Turning this into a function would be fairly straightforward to do.

注意事項:

  • 不考慮時間戳.但您已經(jīng)知道如何做到這一點.
  • 不處理假期.(可以通過使用一系列假期并將其添加到過濾掉星期六和星期日的位置來輕松添加).
  • 需要 PHP 5.3.6+
  • 假設每天工作 8 小時.如果員工不吃午飯,請將 $hours = $days * 8; 更改為 $hours = $days * 8.5;

.

<?php
// Initial datetimes
$date1 = new DateTime('2012-03-22 11:29:16');
$date2 = new DateTime('2012-03-24 03:58:58');

// Set first datetime to midnight of next day
$start = clone $date1;
$start->modify('+1 day');
$start->modify('midnight');

// Set second datetime to midnight of that day
$end = clone $date2;
$end->modify('midnight');

// Count the number of full days between both dates
$days = 0;

// Loop through each day between two dates
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
    // If it is a weekend don't count it
    if (!in_array($dt->format('l'), array('Saturday', 'Sunday'))) {
        $days++;
    }
}

// Assume 8 hour workdays
$hours = $days * 8;

// Get the number of hours worked on the first day
$date1->modify('5:30 PM');
$diff = $date1->diff($start);
$hours += $diff->h;

// Get the number of hours worked the second day
$date1->modify('8 AM');
$diff = $date2->diff($end);
$hours += $diff->h;

echo $hours;

查看實際操作

參考

  • 日期時間類
  • DatePeriod 類
  • DateInterval 類

這篇關于計算PHP中兩個日期之間的工作時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 伦理片97 | 久久av一区 | 久久久久成人精品免费播放动漫 | 91精品成人久久 | 国产高清一区 | 看羞羞视频免费 | 欧美一区二区在线 | 一级特黄视频 | 1级毛片| 成人九色 | 亚洲一区二区三区在线 | 欧美在线一区二区三区四区 | 在线国产视频 | 欧美性生活网 | 日本精品视频在线观看 | 欧美专区在线视频 | 尤物视频在线免费观看 | 爱操影视 | 在线观看国产三级 | 九九热精品在线 | 欧美综合色| 久久久精品一区二区 | 国产特黄一级 | 日韩精品一区二区三区中文在线 | 国产亚洲精品精品国产亚洲综合 | 日韩av福利在线观看 | 国产精品一级 | 中文字幕精品一区二区三区精品 | 国产一级免费视频 | 99国产精品99久久久久久 | 成人高清视频在线观看 | 天天爱爱网 | www.99热这里只有精品 | 久久99深爱久久99精品 | 91大神新作在线观看 | 欧美专区在线 | 成人在线观看欧美 | 东京av男人的天堂 | 久久亚洲国产精品日日av夜夜 | 日日综合 | 国产一区二区精品自拍 |