繁体   English   中英

根据星期几显示文字

[英]Display text based on day of the week

我正在尝试运行一些代码,该代码在一周的任何一天都将返回一个字符串。 如果您有兴趣,我的目标是解决办公室轮到谁控制广播的纠纷:)有点有趣

现在,如果我只有5个人来分配天数,这将非常简单-我可以将每个人绑定到1-5,然后将其与date ('N');的返回值date ('N');匹配date ('N'); 但是我有8个人要分发给 我要分配给某人的唯一日子是工作日(1-5),而不是周末。 分布可以是顺序的,不需要随机分配。

任何人都知道如何实现这一目标?

function number_of_working_days($from, $to) {
    $colleagues     = ['John','Bill','Philip','Mary','Ann','Mark','George','Barry'];
    $workingDays    = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, ...)
    $holidayDays    = ['*-12-25', '*-01-01', '2013-12-23']; # variable and fixed holidays

    $from = new DateTime($from);
    $to = new DateTime($to);
    $to->modify('+1 day');
    $interval = new DateInterval('P1D');
    $periods = new DatePeriod($from, $interval, $to);

    $result = array();
    $c_temp = $colleagues;
    foreach ($periods as $period) {
        if (!in_array($period->format('N'), $workingDays)) continue;
        if (in_array($period->format('Y-m-d'), $holidayDays)) continue;
        if (in_array($period->format('*-m-d'), $holidayDays)) continue;

        if(sizeof($c_temp)==0){
            $c_temp = $colleagues;
        }
        shuffle($c_temp);
        $result[$period->format('Y-m-d')] = array_pop($c_temp);
    }
    return $result;
}

foreach(number_of_working_days('2015-08-01', '2015-08-31') as $date => $colleague){
    echo $date . ": " . $colleague . "\n";
}

输出将如下所示:

2015-08-03: Barry
2015-08-04: Bill
2015-08-05: Mark
2015-08-06: John
2015-08-07: George
2015-08-10: Philip
2015-08-11: Ann
2015-08-12: Mary
2015-08-13: Ann
2015-08-14: John
2015-08-17: Mary
2015-08-18: Barry
2015-08-19: Bill
2015-08-20: George
2015-08-21: Philip
2015-08-24: Mark
2015-08-25: Barry
2015-08-26: Ann
2015-08-27: Mary
2015-08-28: Philip
2015-08-31: George

您可以根据需要插入任意数量的人,开始日期,并且可以根据需要添加假期。

<?php

// add as much ppeople as you like
$people = array('Peter', 'Mike', 'Alice', 'Aaron', 'Omar', 'Hank', 'Wade', 'Zack');

// Here you can add holidays
$holidays = array('2012-07-12', '2012-07-7');

//set start time once
$start = new DateTime('2015-07-7');

// Because the end date need to be + 1(bug?)
$start->modify('+1 day');

// set date today
$end = new DateTime('2015-07-13');
// otherwise the  end date is excluded (bug?)
$end->modify('+1 day');

// total days
$interval = $end->diff($start);
$days = $interval->days;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

foreach($period as $dt) {
    $curr = $dt->format('D');

    // for the updated question
    if (in_array($dt->format('Y-m-d'), $holidays)) {
       $days--;
    }

    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
    }
}

$days = $days % count($people);
echo $people[$days] . " may controle the radio today";

?>

如果必须在开始新一轮比赛之前分配所有人员,则可以执行以下操作:

<?php
$array = array(...); // Set everybody in an Array. This could be getted on the database
$count = count($array); // Count the array. 
For( $i = 0; $i < $count; $i++){
  $result[$i] = array_rand($array, 1); // This gives you a random number of the person who must be assigned
  unset(array[$result]); //Delete from the variable.
}
// at the end, the $array array must be empty.
?>

在$ result变量中,您具有随机顺序。 然后,您必须在一天中分配它们。

<?php
$dayCount = 0; // Count the days from today to assign
Foreach($result as $key=>$res){ 
  While( date('N') + dayCount < 6 ){ // If the day is upper than 6, its saturday or sunday
    if( date('N') + dayCount > date('N') { //Verify that
       $array[$key] = $res . date('Y/M/D'); // if its a working day, save it to $array variable
    }
    $dayCount++; // Increment the Count.
  }
}
//This program is for only one time execution.
print_r($array);
?>

祝您一切顺利。

(请先进行测试,然后再确定代码是否具有100%的功能)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM