Reputation: 514
I'm trying to get some code running, that on any day of the week will return a string. If you're interested, I aim to solve office disputes over who's turn it is to control the radio :) just a bit of fun
Now this would be quite simple, if I had just 5 people to distribute days to - I could just bind each person to a number 1-5 and match it to the returning value of date ('N');
however I have 8 people to distribute to. The only days I want to distribute to a person are weekdays (1-5) and not weekends. The distribution can be sequential, no need for random assignment.
Anyone got any ideas how this could be achieved?
Upvotes: 1
Views: 425
Reputation: 1540
You can just insert as much people as you need, a starting date and you can add holidays if you like.
<?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";
?>
Upvotes: 2
Reputation: 174
If all people must be assigned before starting a new round you could do the following:
<?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.
?>
In the $result variable you have in random order. Then you must assign them on a day.
<?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);
?>
I wish this runs for you.
(PLEASE TEST BEFORE, I AM NOT ASSURE THAT THE CODE IS 100% FUNCTIONAL)
Upvotes: 0
Reputation: 31
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";
}
The output will be like this:
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
Upvotes: 2