根据星期几显示文本

时间:2015-07-17 08:32:06

标签: php date

我正在尝试运行一些代码,在一周的任何一天都会返回一个字符串。如果你有兴趣,我的目标是解决办公室纠纷,不管是谁控制收音机:)只是有点乐趣

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

任何人都有任何想法如何实现这一目标?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

您可以根据需要插入尽可能多的人,开始日期,如果愿意,可以添加假期。

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

?>

答案 2 :(得分:0)

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

<?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%功能)