【问题标题】:Splitting weekday and weekend hours from start and end date not working for different weeks从开始日期和结束日期拆分工作日和周末时间不适用于不同的星期
【发布时间】:2016-07-26 09:11:31
【问题描述】:

需要找出周末的总小时数,即周五、周六和周日以及其他工作日。 使用以下代码,但它没有产生预期的结果。

$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)
它不会进入循环。

知道如何计算小时数吗?

【问题讨论】:

  • 您的计算是否受到一周的限制,或者可能超过一周/月/年?
  • 它可以是任何东西@Wizard
  • 您的解决方案过于复杂,并且存在问题,例如,您的 $start_day 总是在循环内递增,如果它是 6,那么您将有 7 用于下一次迭代。跨度>
  • @Wizard 如何使其成为一个简单的解决方案并计算小时数

标签: php codeigniter datetime


【解决方案1】:

新代码如下(清理它并将比较运算符更改为

    <?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;

【讨论】:

  • 为什么在 endtime (60*60*24*9) @jeff 中添加这个
  • 这是任意的(只是为了设置变量) - 60*60*24 = 1 天,以秒为单位。所以,60*60*24*9 = 9 天。
  • 将我的输出添加到编写的代码中,但总小时数计算为 73。它额外增加 1 小时
  • 发布你添加的内容,我去看看。
  • 我的意思是——无论你在代码中添加了什么*,我都希望你分享,以便我可以帮助你调试。当我运行这个时,我得到了周末 72 小时的正确答案。
【解决方案2】:

不是那么漂亮的代码,但它可以工作:

注意:日期以年为限

<?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;

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多