从开始和结束日期分割的工作日和周末时间不适用于不同的周

时间:2016-07-26 09:11:31

标签: php codeigniter datetime

需要找到星期五,星期六和星期日以及其他工作日的周末总时数。 使用下面的代码,但它没有产生所需的结果。

$start_day = date('w', $start_time);
        $start_hour = date('G', $start_time);
        $end_day = date('w', $end_time);
        $end_hour = date('G', $end_time);
        $normal_hours = $surge_hours = 0;


        for (;($start_hour < 24 && $start_day <= $end_day); $start_hour++) {

            switch ($start_day) {
                case 0: $surge_hours++;
                    break;
                case 5: $surge_hours++;
                    break;
                case 6: $surge_hours++;
                    break;
                case 1: $normal_hours++;
                    break;
                case 2: $normal_hours++;
                    break;
                case 3: $normal_hours++;
                    break;
                case 4: $normal_hours++;
                    break;
            }
            echo __line__;
            echo "<pre>";
            print_r('surgehours'.$surge_hours);
            echo "<br/>";
            print_r('normalhours'.$normal_hours);
            echo "<pre/>";
            if ($start_hour == 23) {
                $start_hour = 0;
                $start_day++;
            }

            if ($start_day == $end_day) {
                if ($start_hour == $end_hour)
                    break;
            }
        }

未使用案例:

如果开始日是星期五,即:7月29日和结束日是8月1日($start_day = 5 and $end_day = 1
它没有进入循环。

任何想法如何计算小时数?

2 个答案:

答案 0 :(得分:1)

新代码在下面(清理它并将比较运算符更改为&lt;而不是&lt; =这阻止了最后一小时的通过):

    <?php
$start_time = strtotime('next friday 11am');
$end_time = $start_time + (60*60*24*3);

function allocate_hour($timestamp) {
    if(date('w',$timestamp) >= 5 OR date('w',$timestamp) == 0) {
        return 0;
    } else { return 1; }
}

$weekdayhours = 0;
$weekendhours = 0;
for($thistime=$start_time;$thistime<$end_time;$thistime+=3600){
    if(allocate_hour($thistime) === 1) { $weekdayhours++; } else { $weekendhours++; } // add 1 for each hour.
}

echo 'Weekday hours: '.$weekdayhours;
echo '<BR>weekend hours: '.$weekendhours;

答案 1 :(得分:0)

不是那么漂亮的代码,但它确实有效:

  

注意:日期受年份限制

<?php

// test values
$start_date = mktime(9,0,0,5,5,2016);   //  5-may-2016 (Thursday)
$end_date   = mktime(18,0,0,5,20,2016); // 20-may-2016 (Saturday)

// count days between dates
$days_between  = date('z',$end_date) - date('z', $start_date) + 1;
// guessing how many weekends days can be
$weekdays = floor($days_between/7)*3;

// get weekday nubmer 1-monday..7-sunday
$sday = date('N',$start_date);
$eday = date('N',$end_date);

// initialize counters
$surge = 0;
$normal = 0;

if ($days_between >=7) {
    // correct weekends days count
    // when we have more than 1 week difference
    switch ($sday) {
        case 7:
            $weekdays--;
        case 6:
            $weekdays--;
    }

    switch ($eday) {
        case 7:
            $weekdays++;
        case 6:
            $weekdays++;
        case 5:
            $weekdays++;
    }
} else {
    // correct weekends days count
    // when we have less than 1 week difference
    switch ($sday) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            $weekdays++;
        case 6:
            $weekdays++;
        case 7:
            $weekdays++;
    }
    switch ($eday) {
        case 5:
            $weekdays--;
        case 6:
            $weekdays--;
    }
}

// remember start and end hours
$shour = date('G', $start_date);
$ehour = date('G', $end_date);

if ($sday == $eday) {
    // we're start and end at the same day
    if (in_array($sday, array(5,6,7))) {
        $surge += $ehour - $shour;
    } else {
        $normal += $ehour - $shour;
    }
} else {
    $first_day_hours = 24 - $shour;
    $last_day_hours  = $ehour;
    // decrease counter by two, because
    // the first and last days process separately
    $days_between -= 2;

    if (in_array($sday, array(5,6,7))) {
        $surge += $first_day_hours;
        $weekdays--;
    } else {
        $normal += $first_day_hours;
    }

    if (in_array($eday, array(5,6,7))) {
        $surge += $last_day_hours;
        $weekdays--;
    } else {
        $normal += $last_day_hours;
    }

    if ($days_between>0) {
        $surge += 24 * $weekdays;
        $normal += 24 * ($days_between - $weekdays);
    }
}


echo 'Normal: '.$normal.PHP_EOL;
echo ' Surge: '.$surge.PHP_EOL;

靠近源的代码(使用循环):

  

注意:日期不受限制

<?php

// test values
$start_date = mktime(9,0,0,5,5,2016);
$end_date   = mktime(18,0,0,5,20,2016);

// remember start and end hours
$shour = date('G', $start_date);
$ehour = date('G', $end_date);
$first_day_hours = 24 - $shour;
$last_day_hours  = $ehour;

$sdate = mktime(0,0,0,date('m',$start_date),date('d',$start_date)+1,date('Y',$start_date));
$edate = mktime(0,0,0,date('m',$end_date),date('d',$end_date)-1,date('Y',$end_date));

// initialize counters
$surge = 0;
$normal = 0;

if (in_array(date('N',$start_date), array(5,6,7))) {
    $surge += $first_day_hours;
} else {
    $normal += $first_day_hours;
}
if (in_array(date('N',$end_date), array(5,6,7))) {
    $surge += $last_day_hours;
} else {
    $normal += $last_day_hours;
}
while ($sdate<=$edate) {
    if (in_array(date('N',$sdate), array(5,6,7))) {
        $surge += 24;
    } else {
        $normal += 24;
    }
    $sdate = mktime(0,0,0,date('m',$sdate),date('d',$sdate)+1,date('Y',$sdate));
}
echo 'Normal: '.$normal.PHP_EOL;
echo ' Surge: '.$surge.PHP_EOL;