How to check for conflict with multiple datetime RANGES - php

I am stumped about how to go about checking to see if multiple *date/time value -ranges- (meaning: start and end times) are in conflict with one another.
Summary & samples of data I need to work with.
I have an (app) events table, that is used to create a form for the user to go through and check off (checkboxes) all the sessions/events they attended.
The names of the checkboxes are generated using info about the session. (month/day start/end times along with a session id)
ie: hours_9_5_p02_800_845
9 = month
5 = date
p02 = session id/event code
800 = start time
845 = end time
all delimited by an underscore ("_")
If the user checks (and submits) multiple sessions, there will be many of these values to check for a time conflict..
hours_9_5_p08_755_800
hours_9_5_p02_800_845
hours_9_5_p02_800_855
hours_9_5_p03_800_845
hours_9_5_p04_820_835
hours_9_5_p04_845_900
hours_9_5_REG_900_915
hours_9_5_REG_1300_1305
hours_9_5_REG_1310_1335
hours_9_5_REG_1320_1335
The above is an example of the fieldlist/array that I 'could' get as a user selection/submission that I need to check for any possible conflicts (obviously the user couldnt be two places at once) :) And the above have many/several overlapping of just the same exact time slots selected.
** I am open to either PHP, (checking after the user submits) or javascript/jQuery (if it can do the date/time RANGE conflict checking, it might be easier to then highlight those rows/elements on the page if done on the front end)
I'd image, first you need to parse those checkbox names/strings from the fieldlist array...
which I have done like so: (php)
function conflictParse($delimiter, $targetString){
//echo 'fired';
$breakDown = explode($delimiter, $targetString);
$startTime = substr_replace($breakDown[4] , ':', -2, 0);
$endTime = substr_replace($breakDown[5] , ':', -2, 0);
$startString = "$breakDown[1]/$breakDown[2]/2015 $startTime";
$endString = "$breakDown[1]/$breakDown[2]/2015 $endTime";
$startFormat = strtotime($startString);
$endFormat = strtotime($endString);
$start = date('m/d/Y G:i',$startFormat);
$end = date('m/d/Y G:i',$endFormat);
return "Session Times: $start -- $end <br>";
}
echo conflictParse('_','hours_9_5_p02_800_845');
but I am not clear on HOW to go about using this RANGE of a date start & end time to check against MULTIPLE other date start/end time RANGES?
maybe just sticking with having PHP parse/check conflict upon submit and then return some array of the (original) names page to the page (for some jQuery to use and highlight the elements..etc (but I can handle that aspect later.. for right now I am need help on how I can get the above parse 'date/time' start/end range values checked for conflicts against other 'date/time' start/end range values
update:
Here is the current nested associative array I have to work with for comparing:
Array (
[0] => Array (
[id] => hours_9_9_p02_800_845
[fullStart] => 09/09/2015 8:00
[fullEnd] => 09/09/2015 8:45
[month] => 9
[date] => 9
[session_code] => p02
[start] => 8:00
[end] => 8:45
[hasConflict] => false
)
[1] => Array (
[id] => hours_9_9_p02_800_855
[fullStart] => 09/09/2015 8:00
[fullEnd] => 09/09/2015 8:55
[month] => 9
[date] => 9
[session_code] => p02
[start] => 8:00
[end] => 8:55
[hasConflict] => false
)
[2] => Array (
[id] => hours_9_9_p03_800_845
[fullStart] => 09/09/2015 8:00
[fullEnd] => 09/09/2015 8:45
[month] => 9
[date] => 9
[session_code] => p03
[start] => 8:00
[end] => 8:45
[hasConflict] => false
)
[3] => Array (
[id] => hours_9_9_p04_820_830
[fullStart] => 09/09/2015 8:20
[fullEnd] => 09/09/2015 8:30
[month] => 9
[date] => 9
[session_code] => p04
[start] => 8:20
[end] => 8:30
[hasConflict] => false
)
[4] => Array (
[id] => hours_9_9_p04_845_900
[fullStart] => 09/09/2015 8:45
[fullEnd] => 09/09/2015 9:00
[month] => 9
[date] => 9
[session_code] => p04
[start] => 8:45
[end] => 9:00
[hasConflict] => false
)
[5] => Array (
[id] => hours_9_9_REG_1300_1315
[fullStart] => 09/09/2015 13:00
[fullEnd] => 09/09/2015 13:15
[month] => 9
[date] => 9
[session_code] => REG
[start] => 13:00
[end] => 13:15
[hasConflict] => false
)
[6] => Array (
[id] => hours_9_9_REG_1300_1330
[fullStart] => 09/09/2015 13:00
[fullEnd] => 09/09/2015 13:30
[month] => 9
[date] => 9
[session_code] => REG
[start] => 13:00
[end] => 13:30
[hasConflict] => false
)
)
I need to convert your js functions over to PHP and of course use the fullStart/fullEnd variables in my time compares I guess..??
(but your function is still confusing me as I see references to event1, event 2.. (to match your example)..
update 2:
The above is my object/array (associative array) that I got from selecting some check boxes, and submitting my form...
Here is my attempt to convert your JS code to PHP based [with some update variablenames]: (and the commented out lines just to try and get some sort of output somewhere)
print_r($conflict_list);
function checkFirst($cf_presX, $cf_presY) {
//$cf_presX['fullStart'] < $cf_presY['fallStart'] ? checkConflict($cf_presX, $cf_presY) : checkConflict($cf_presY, $cf_presX);
echo 'Pres Check: '.$cf_presX[0] . '<br>';
echo 'Pres Check: '.$cf_presY[0] . '<br>';
/*
function checkConflict ($cc_presX, $cc_presY) {
if ($cc_presX.['fullEnd'] > $cc_presY.['fullStart']) {
$cc_presX.['hasConflict'] = true;
$cc_presY.['hasConflict'] = true;
}
}
*/
}
function setConflicts($events) {
for ($i = 0; $i < count($events); $i++) {
for ($j = 0; $i < count($events); $j++) {
// if it is not the same event
// if (i !== j) is the same $age['Peter']
if ($events[$i]['id'] !== $events[$j]['id']) {
checkFirst($events[$i], $events[$j]);
}
}
}
}
setConflicts($conflict_list);
I just keep getting a loop with undefined offset: (counting up to the 100k+ mark)
Notice: Undefined offset: 0 in
C:\wamp\www\projects\misc\conflict_check_new.php on line 49 Pres
Check:
Notice: Undefined offset: 0 in
C:\wamp\www\projects\misc\conflict_check_new.php on line 50 Pres
Check:
Notice: Undefined offset: 0 in
C:\wamp\www\projects\misc\conflict_check_new.php on line 49 Pres
Check:

The same logic could apply in PHP, but assuming you can get your events out into JavaScript and create an array of objects with start and end dates like so:
var events = [
{
id: 'event1',
start: new Date('1/1/1 5:00'),
end: new Date('1/1/1 6:00'),
hasConflict: false
},
{
id: 'event2',
start: new Date('1/1/1 5:30'),
end: new Date('1/1/1 6:30'),
hasConflict: false
},
{
id: 'event3',
start: new Date('1/1/1 7:30'),
end: new Date('1/1/1 8:30'),
hasConflict: false
}
]
You can compare events to see if the one that starts first, has an end time that's later the second one's start time.
function checkFirst (event1, event2) {
event1.start < event2.start
? checkConflict(event1, event2)
: checkConflict(event2, event1)
function checkConflict (first, second) {
if (first.end > second.start) {
first.hasConflict = second.hasConflict = true
}
}
}
Then you can check events against each other. Here's a not particularly efficient, but at least suitable loop:
function flagAllEventsWithConflicts (events) {
events.forEach(event1 => {
events.forEach(event2 => {
event1.id !== event2.id && checkFirst(event1, event2)
})
})
}
Update: The above function can also be written as a nested for loop:
function flagAllEventsWithConflicts (events) {
for (var i = 0; i < events.length; i++) {
for (var j = 0; j < events.length; j++ {
// if it is not the same event
// if (i !== j) is the same
if (events[i].id !== events[j].id) {
checkFirst(events[i], events[j])
}
}
}
}
Then check to see if hasConflict is true or false:
flagAllEventsWithConflicts(events)
console.table(events)
Run this fiddle and checkout the console

Related

Having trouble wrapping my head around how to write a particular loop

I am querying a SQL database to return open work order operations.
My query is producing the Work Order number, operation status and due date.
I am trying to figure out how to iterate through the array that is returned and:
Gather the sum of operations due within a week
Gather the sum of operations due in the second week
Continue to do this until I have made it through all of the entries, ongoing for as many weeks as necessary.
My SQL query looks something like:
SELECT * FROM OPERATION WHERE RESOURCE_ID = '280LASERS' ORDER BY DUE_DATE;
It will return something like:
W/O # | Setup Hours | Run Hours | Due Date
W159769 | 0.5 | 15.0 | 03/01/2020
W159770 | 1.5 | 9.0 | 04/01/2020
W159771 | 0.75 | 81.0 | 05/01/2020
Either way, what I am trying to accomplish is, query the database, step through my result and get the sum foreach week. While NOW+7days <= DUE_DATE; While NOW+14days <= DUE_DATE...
Week One = 15.5 Hours; Week Two = 10.5 Hours; Week Three = 81.75 Hours
EDIT: I apologize for my mess of a question, this is one of the more intense tasks I have tried to accomplish with SQL and PHP.
We are trying to get a better handle on our capacity, and reporting on our capacity.I am hoping to be able to run a query that pulls all of the '280LASERS' Operations and have some sort of root value (Like todays Date) to compare the DUE_DATE against.My plan is to sort by DUE_DATE and get the SUM(SETUP_HRS + RUN_HRS) until DUE_DATE is greater than (TODAY() + 7) then, get the SUM(SETUP_HRS + RUN_HRS) until DUE_DATE is greater than (TODAY() + 14) then ... I can't achieve this with static variables because the number of weeks can go from 6 weeks out, to more than 30 weeks out, simply depending on the DUE_DATE of the furthest out order.I am so close I can taste it, I would really like to share my code, and the output... but feel I have blown this page up and it is a hot mess. Would it be acceptable for me to delete everything above this and reshare my code as it is, as well as the output I am getting.
I don't think you need a loop. It looks like you could just calculate the week number, group by that, and sum the hours for the week.
MySQL
select
DATEDIFF(due_date, NOW()) DIV 7 + 1 AS week_number,
SUM(setup_hours + run_hours) AS week_hours
from operation
where resource_id = '280LASERS'
group by week_number;
SQL Server
select
DATEDIFF(wk, getdate(), due_date) AS week_number,
SUM(setup_hours + run_hours) AS week_hours
from operation
where resource_id = '280LASERS'
group by DATEDIFF(wk, getdate(), due_date);
so I'm back, I'll add a better commented code, here: can't edit the old answer, since I deleted my account and forgot to cancel :|
Anyways, you asked how to manipulate the data. It's a simple array and all the inner arrays are sums from the start of the week to the end. Now, you could store them with different keys, I just used the default assigning because of simplicity.
$results=array(
array( 'due_date'=>'12/02/2020', 'run_hours'=>12.4, 'setup_hours'=>2.4, ), // 2020-02-12 00:00:00
array( 'due_date'=>'15/02/2020', 'run_hours'=>10.4, 'setup_hours'=>1.4, ), // 2020-02-15 00:00:00
array( 'due_date'=>'18/02/2020', 'run_hours'=>8.4, 'setup_hours'=>3.4, ), // 2020-02-18 00:00:00
array( 'due_date'=>'20/02/2020', 'run_hours'=>2.4, 'setup_hours'=>1.4, ), // 2020-02-20 00:00:00
array( 'due_date'=>'21/02/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), // 2020-02-21 00:00:00
array( 'due_date'=>'24/02/2020', 'run_hours'=>12.4, 'setup_hours'=>1.4, ), // 2020-02-24 00:00:00
array( 'due_date'=>'26/02/2020', 'run_hours'=>11.3, 'setup_hours'=>1.4, ), // 2020-02-26 00:00:00
array( 'due_date'=>'29/02/2020', 'run_hours'=>4.4, 'setup_hours'=>2.4, ), // 2020-02-29 00:00:00
array( 'due_date'=>'02/03/2020', 'run_hours'=>5.7, 'setup_hours'=>4, ), // 2020-03-02 00:00:00
array( 'due_date'=>'04/03/2020', 'run_hours'=>11.5, 'setup_hours'=>3.4, ), // 2020-03-04 00:00:00
array( 'due_date'=>'06/03/2020', 'run_hours'=>7.3, 'setup_hours'=>1.4, ), // 2020-03-06 00:00:00
array( 'due_date'=>'08/03/2020', 'run_hours'=>9.6, 'setup_hours'=>1.4, ), // 2020-03-08 00:00:00
array( 'due_date'=>'12/03/2020', 'run_hours'=>14.7, 'setup_hours'=>1.4, ), // 2020-03-12 00:00:00
array( 'due_date'=>'15/03/2020', 'run_hours'=>12.5, 'setup_hours'=>1.4, ), // 2020-03-15 00:00:00
array( 'due_date'=>'19/03/2020', 'run_hours'=>4.4, 'setup_hours'=>1.4, ), // 2020-03-19 00:00:00
array( 'due_date'=>'21/03/2020', 'run_hours'=>5.6, 'setup_hours'=>4, ), // 2020-03-21 00:00:00
array( 'due_date'=>'24/03/2020', 'run_hours'=>11.4, 'setup_hours'=>1.4, ), // 2020-03-24 00:00:00
array( 'due_date'=>'29/03/2020', 'run_hours'=>7.4, 'setup_hours'=>1.4, ), // 2020-03-29 00:00:00
array( 'due_date'=>'01/04/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), // 2020-04-01 00:00:00
// some far off weeks
array( 'due_date'=>'18/06/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
array( 'due_date'=>'21/06/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
array( 'due_date'=>'09/07/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
array( 'due_date'=>'12/08/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ),
);
$time=strtotime(date('Y-m-d')); // get time in same
// wrapping time in strtotime and date trims the seconds to the desired format
$one_week=60*60*24*7;
$sums=array();
foreach($results as $row){
/* php 5.3+ this block of code gets the time of the date, this conversions are made in case a custom non standard date format is made, alternatively one can use strtotime with the correct date format
$date = DateTime::createFromFormat('d/m/Y', $row['due_date']);//use your format and values
if(!$date){
echo 'Not a valid format';
break;
}
$entry_time = strtotime(date('Y-m-d',$date->getTimestamp()));
// if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
// this may not be desired, so this wrapping it in strtotime and date trims the values
*/
// WARNING: If the format is right weeks will be way off
$entry_time = strtotime($row['due_date']); // if due_date is a valid format, see PHP docs for more information
if (!$entry_time) {
echo "Not a valid date format";
break;
}
$entry_work_hours=$row['run_hours']+$row['setup_hours'];
// if the entry time is by some reason smaller then the current time save it to a special past_due container
if ($entry_time < $time) {
// if a past_due container exists add the sum, otherwise create a past_due container
if (isset($sums['past_due'])) {
$sums['past_due']['sum']+=$entry_work_hours;
} else {
$sums['past_due']= array(
'sum' => $entry_work_hours,
'start' => $row['due_date'], // the earliest event
'end' => date('d/m/Y',$time), // current time, if $entry_time is bigger or equal we're talking about entries that are yet to happen
);
}
} else if ( $entry_time >= $time ){
// getting the future_dues array, every object holds an array/map, that holds the sum, the start of the week and when the week ends
// endings are exclusive ie. if an entry_data falls on the end date it goes to the start of the next container
if (isset($sums['future_dues'])) {
$future_dues=$sums['future_dues'];
} else {
$future_dues = array(
array(
'sum' => 0,
'start' => $time,
'end' => $time+$one_week
)
);
}
// get the last week container, and save the key so we can reassign it back to the $sums array on the right spot
$last_index = count($future_dues)-1;
$future_due = $future_dues[$last_index];
// manipulate the week data
// if the entry time is smaller then the current end of the week add to the sum, otherwise add a new week interval container
if ($entry_time < $future_due['end']) {
$future_due['sum']+=$entry_work_hours;
// reassign week container
$future_dues[$last_index]=$future_due;
} else {
$last_week_end = $future_due['end'];
$new_end = $last_week_end + $one_week;
//do a while loop to get the next week end in which the work is done
while ($new_end < $entry_time) {
// skip this part if empty weeks are not desired
$future_dues[] = array(
'sum' => 0,
'start' => $last_week_end,
'end' => $new_end
);
$last_week_end = $new_end;
$new_end = $new_end + $one_week;
// echo "$new_end < $entry_time".'<br>';
}
// add a new week container, the start of the week is the end of the previous one and the end is 7 days from that
$future_dues[]=array(
'sum' => $entry_work_hours,
'start' => $last_week_end,
'end' => $new_end
);
}
// reassign the whole week containers container to the array
$sums['future_dues']=$future_dues;
}
}
// convert time back to dates
foreach ($sums['future_dues'] as $key => &$due) {
$due['start']=date('d/m/Y',$due['start']);
$due['end']=date('d/m/Y',$due['end']);
}
// use $sums to display the values you need, use:
// echo "<pre>";
// print_r($sums);
// echo "</pre>";
// to better understand how data is stored
echo "<pre>"; // use pre tags to have a nice inline values, this can be rewriten into a table
$past_due=$sums['past_due'];
//past due is a single container
$time_prefix="Time: ";
$working_hours_prefix="Working hours: ";
$time = $time_prefix.$sums['past_due']['start']." - ".$sums['past_due']['end'];
echo $time."<br>";
echo $working_hours_prefix.str_pad($sums['past_due']['sum'],abs(strlen($time)-strlen($working_hours_prefix)),' ',STR_PAD_LEFT);
// make it inline with the time
echo "<br><br>";
$due_dates=$sums['future_dues'];
foreach($due_dates as $week_container){
$time = $time_prefix.$week_container['start']." - ".$week_container['end'];
echo $time."<br>";
echo $working_hours_prefix.str_pad($week_container['sum'],abs(strlen($time)-strlen($working_hours_prefix)),' ',STR_PAD_LEFT);
echo "<br><br>";
//echo $week_container['sum']; /// if you want to show the sum
//echo $week_container['start']; /// if you want to show the start
//echo $week_container['end']; /// if you want to show the end
}
echo "</pre>";
// above is a bit abstracted but it esencially does this
echo "<br><br>";
echo "<br><br>";
$past_due=$sums['past_due'];
$past_start = $sums['past_due']['start'];
$past_end = $sums['past_due']['end'];
$past_sum = $sums['past_due']['sum'];
echo "Time: $past_start - $past_end<br>";
echo "Working hours: $past_sum"; // previous case adds breaks to be inline
echo "<br><br>";
$due_dates=$sums['future_dues'];
foreach($due_dates as $week_container){
$week_start = $week_container['start'];
$week_end = $week_container['end'];
$week_sum = $week_container['sum'];
echo "Time: $week_start - $week_end<br>";
echo "Working hours: $week_sum"; // previous case adds breaks to be inline
echo "<br><br>";
}
Edit:
A new while loop was added to account for empty weeks.
Note d/m/Y is not strtotime recognised format and it will be read as m/d/Y. To convert it refer to this question.
Edit-2:
To answer your comment. Ok so the thing about the spans is that I made them so that if the span went from 2020-01-01 to 2020-01-08 and the second one from 2020-01-08 to 2020-01-15 where should the working hours of 2020-01-08 go to week 1 or week 2?
When you corrected $entry_time < $future_due['end'] to $entry_time <= $future_due['end'] this means that the count is added to week 1, while the original solution would have added it to week 2 as the starting date.
You can try and add 8 days and then subtract one if you wanted the containers to span between 2020-01-01 and 2020-01-08 and 2020-01-09 and 2020-01-16 and have both endings be inclusive. Now, I'm not going to write this part since it really depends on how YOU want define your endings.
And your question if you can change the time to something else then the current time? Sure, just change this line.
$time=strtotime(date('Y-m-d'));
//to
$time=__TIME__YOU_WANT_IN_SECONDS__;
//or
$time = strtotime(__THE_DATE_YOU_WANT__); // eg. 01/01/2020
// now this is the time to compare all other dates to
As Don't Panic pointed out about the due dates, I think you might be displaying it in a dd/mm/YYYY format and I'll work from there.
I'll work with a foreach loop but I think Don't Panic's solution might be more efficient.
Steps:
1. Loop through the values and calculate the time of the date,
2. Compare it with the desired date
3. Add it to the right sum
Notes: date format is important, if hours, minutes and seconds are missing the getTimestamp adds the current values for them
I've added some test data based on my understanding of the problem.
I've also added a past due column in case any date is already past the current time().
The foreach loop checks the if the entry_time is smaller than the current time, this means it is past_due.
If not, we check if the future_dues are set. Future dues represent all the different weeks in the future. If none are set one array with an array is added.
The second array represent the current closest future starting from the current time to the end of 7 days in the future.
Start and end help us read the end results better, they are timestamps. Also a sum key of value 0 is added.
Then we take the last element from the future_dues and see if the due_date is smaller then the end of the week. If it is we add the working hour, otherwise we add a new future_due object.
At the end I added a foreach loop that converts the timestamps to a date format.
$results=array(
array( 'due_date'=>'12/02/2020', 'run_hours'=>12.4, 'setup_hours'=>2.4, ), // 2020-02-12 00:00:00
array( 'due_date'=>'15/02/2020', 'run_hours'=>10.4, 'setup_hours'=>1.4, ), // 2020-02-15 00:00:00
array( 'due_date'=>'18/02/2020', 'run_hours'=>8.4, 'setup_hours'=>3.4, ), // 2020-02-18 00:00:00
array( 'due_date'=>'20/02/2020', 'run_hours'=>2.4, 'setup_hours'=>1.4, ), // 2020-02-20 00:00:00
array( 'due_date'=>'21/02/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), // 2020-02-21 00:00:00
array( 'due_date'=>'24/02/2020', 'run_hours'=>12.4, 'setup_hours'=>1.4, ), // 2020-02-24 00:00:00
array( 'due_date'=>'26/02/2020', 'run_hours'=>11.3, 'setup_hours'=>1.4, ), // 2020-02-26 00:00:00
array( 'due_date'=>'29/02/2020', 'run_hours'=>4.4, 'setup_hours'=>2.4, ), // 2020-02-29 00:00:00
array( 'due_date'=>'02/03/2020', 'run_hours'=>5.7, 'setup_hours'=>4, ), // 2020-03-02 00:00:00
array( 'due_date'=>'04/03/2020', 'run_hours'=>11.5, 'setup_hours'=>3.4, ), // 2020-03-04 00:00:00
array( 'due_date'=>'06/03/2020', 'run_hours'=>7.3, 'setup_hours'=>1.4, ), // 2020-03-06 00:00:00
array( 'due_date'=>'08/03/2020', 'run_hours'=>9.6, 'setup_hours'=>1.4, ), // 2020-03-08 00:00:00
array( 'due_date'=>'12/03/2020', 'run_hours'=>14.7, 'setup_hours'=>1.4, ), // 2020-03-12 00:00:00
array( 'due_date'=>'15/03/2020', 'run_hours'=>12.5, 'setup_hours'=>1.4, ), // 2020-03-15 00:00:00
array( 'due_date'=>'19/03/2020', 'run_hours'=>4.4, 'setup_hours'=>1.4, ), // 2020-03-19 00:00:00
array( 'due_date'=>'21/03/2020', 'run_hours'=>5.6, 'setup_hours'=>4, ), // 2020-03-21 00:00:00
array( 'due_date'=>'24/03/2020', 'run_hours'=>11.4, 'setup_hours'=>1.4, ), // 2020-03-24 00:00:00
array( 'due_date'=>'29/03/2020', 'run_hours'=>7.4, 'setup_hours'=>1.4, ), // 2020-03-29 00:00:00
array( 'due_date'=>'01/04/2020', 'run_hours'=>9.4, 'setup_hours'=>1.4, ), // 2020-04-01 00:00:00
);
$time=strtotime(date('Y-m-d')); // get time in same
// wrapping time in strtotime and date trims the seconds to the desired format
$one_week=60*60*24*7;
$sums=array();
foreach($results as $row){
$date = DateTime::createFromFormat('d/m/Y', $row['due_date']);//use your format and values
if(!$date){
echo 'Not a valid format';
break;
}
$entry_time = strtotime(date('Y-m-d',$date->getTimestamp()));
// if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
// this may not be desired, so this wrapping it in strtotime and date trims the values
$entry_work_hours=$row['run_hours']+$row['setup_hours'];
if ($entry_time < $time) {
if (isset($sums['future_dues'])) {
$sums['past_due']['sum']+=$entry_work_hours;
} else {
$sums['past_due']= array(
'sum'=> $entry_work_hours,
'start'=> $row['due_date'],
'end' => date('d/m/Y',$time),
);
}
} else if ( $entry_time > $time ){
if (isset($sums['future_dues'])) {
$future_dues=$sums['future_dues'];
} else {
$future_dues = array(
array(
'sum'=>0,
'start'=>$time,
'end'=>$time+$one_week
)
);
}
$last_index = count($future_dues)-1;
$future_due = $future_dues[$last_index];
if ($entry_time < $future_due['end']) {
$future_due['sum']+=$entry_work_hours;
$future_dues[$last_index]=$future_due;
} else {
$future_dues[]=array(
'sum'=>$entry_work_hours,
'start'=>$future_due['end'],
'end'=>$future_due['end']+$one_week
);
}
$sums['future_dues']=$future_dues;
}
}
// if you want to conver them back to dates
foreach ($sums['future_dues'] as $key => &$due) {
$due['start']=date('d/m/Y',$due['start']);
$due['end']=date('d/m/Y',$due['end']);
}
SQL Code seems to get me some good info, here is the final version
SELECT
DATEDIFF(week, getdate(), WORK_ORDER.DESIRED_WANT_DATE) AS week_number,
SUM(OPERATION.SETUP_HRS + OPERATION.RUN_HRS) AS week_hours
FROM OPERATION
JOIN WORK_ORDER ON OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID
WHERE OPERATION.RESOURCE_ID = '103TURRET' AND (OPERATION.STATUS = 'R' OR OPERATION.STATUS = 'F') AND WORK_ORDER.SUB_ID = '0'
GROUP BY DATEDIFF(week, getdate(), WORK_ORDER.DESIRED_WANT_DATE)
ORDER BY week_number;
Gives Me
week_number week_hours
-14 0.630
-11 1.640
-8 1.980
-1 0.540
0 3.820
1 18.500
2 15.090
3 3.410
5 16.490
7 0.890
9 17.950
14 5.000
19 5.000
23 6.750
27 5.000
31 5.000
I manually total the negatives + zero week = past due
Thank you very much #Don't Panic for all of your help.
Ehh... if anyone could help me get this to where it always starts on a Monday, rather than the day it is run, I would appreciate it.
Alright, here is the code I have so far. I would like to get it to look better, maybe not show all the different elements in the array... but I am terrified to change much more.
This is some pretty incredible code as far as I am concerned.
<?php //CONNECTION SETTING
$database='VMFG';
$odbc_name='SQLServer';
$odbc_user='sa';
$odbc_password='Password#';
$con = odbc_connect($odbc_name,$odbc_user,$odbc_password);
$query="SELECT OPERATION.WORKORDER_BASE_ID AS 'W/O #', /*STATEMENT TO PULL OPERATIONS AND PARSE WORK ORDER DATES*/
OPERATION.WORKORDER_SUB_ID AS 'SUB ID',
OPERATION.RESOURCE_ID AS 'RESOURCE ID',
OPERATION.SETUP_HRS AS 'SETUP HRS',
OPERATION.RUN AS 'RUN RATE',
OPERATION.RUN_TYPE AS 'U/M',
OPERATION.RUN_HRS AS 'RUN TOTAL',
OPERATION.CALC_START_QTY AS 'START QTY',
OPERATION.CALC_END_QTY AS 'END QTY',
OPERATION.COMPLETED_QTY AS 'QTY COMP',
OPERATION.DEVIATED_QTY AS 'DIFFERENCE',
OPERATION.ACT_SETUP_HRS AS 'SETUP USED',
OPERATION.ACT_RUN_HRS AS 'HRS RUN',
OPERATION.STATUS,
OPERATION.SETUP_COMPLETED AS 'SETUP COMP',
WORK_ORDER.DESIRED_WANT_DATE AS 'DUE DATE'
FROM OPERATION
JOIN WORK_ORDER ON OPERATION.WORKORDER_BASE_ID=WORK_ORDER.BASE_ID
WHERE (OPERATION.STATUS = 'R' OR OPERATION.STATUS = 'F')
AND (OPERATION.RESOURCE_ID = '".$_GET['operation']."')
AND WORK_ORDER.SUB_ID = '0'
ORDER BY STATUS DESC, [DUE DATE];";
?>
<form method="get"> <?//FORM TO CHOOSE OPERATION?>
<table>
<tr>
<td>
<select name="operation">
<option value="103TURRET">103TURRET</option>
<option value="104PRESSBRAKES">104PRESSBRAKES</option>
<option value="280LASERS">280LASERS</option>
<option value="300WELD">300WELD</option>
<option value="701POWDERLINE">701POWDERLINE</option>
<option value="Outside Service">Outside Server</option>
<option value="ANYOSS">ANY OSS</option>
</select>
</td>
<td><input type="submit" value="Submit" name="action" /></td>
<td><a href='index.php'>Start Over</a></td>
<td><a href='/xampp/mpc_db/index.php'>Return to DB</a></td>
</tr>
</table>
</form>
<?php
$exec = odbc_exec($con, $query);
$results = array();
while ($row = odbc_fetch_array($exec)) {
$results[] = $row;
}
//*****************************************************************
$time=strtotime(date('Y/m/d')); // get time in same
//wrapping time in strtotime and date trims the seconds to the desired format
$one_week=604800;
$sums=array();
foreach($results as $row){
/*$date = DateTime::createFromFormat('Y/m/d', $row['DUE DATE']);//use your format and values*/
$date = date($row['DUE DATE']);
if(!$date){
echo 'Not a valid format';
break;
}
$entry_time = strtotime(date('Y/m/d',strtotime($date)));
// if your date format doesnt have hours minutes and seconds then timestamp will add the current h,min,s,
// this may not be desired, so this wrapping it in strtotime and date trims the values
$entry_work_hours=$row['RUN TOTAL']+$row['SETUP HRS'];
if ($entry_time < $time) {
if (isset($sums['past_due'])) {
$sums['past_due']['sum'] += $entry_work_hours;
} else {
$sums['past_due'] = array(
'sum'=> $entry_work_hours,
'start'=> $row['due_date'],
'end' => date('Y/m/d',$time),
);
}
} else if ( $entry_time > $time ){
if (isset($sums['future_dues'])) {
$future_dues=$sums['future_dues'];
} else {
$future_dues = array(
array(
'sum'=>0,
'start'=>$time,
'end'=>$time+$one_week
)
);
}
$last_index = count($future_dues)-1;
$future_due = $future_dues[$last_index];
if ($entry_time < $future_due['end']) {
$future_due['sum']+=$entry_work_hours;
$future_dues[$last_index]=$future_due;
} else {
$future_dues[]=array(
'sum'=>$entry_work_hours,
'start'=>$future_due['end'],
'end'=>$future_due['end']+$one_week
);
}
$sums['future_dues']=$future_dues;
}
}
// if you want to convert them back to dates
foreach ($future_dues as $key => &$due) {
$due['start']=date('Y/m/d',$due['start']);
$due['end']=date('Y/m/d',$due['end']);
}
//**********************************************************************
foreach($sums['past_due'] as $stuff){ //Actually kind-of sort-of********
print_r($stuff); //Looks like exactly**************
echo "<br>"; //What I am trying to create******
} //Past Due Hours!*****************
//**********************************************************************
//**********************************************************************
foreach($future_dues as $edues){ //Actually kind-of sort-of************
print_r($edues); //Looks like exactly******************
echo "<br>"; //What I am trying to create**********
} //Future Hours!***********************
//**********************************************************************
?>
The best part is! My output is readable!
5.26
2020/02/27
Array ( [sum] => 9.26 [start] => 2020/02/27 [end] => 2020/03/05 )
Array ( [sum] => 7.31 [start] => 2020/03/05 [end] => 2020/03/12 )
Array ( [sum] => 6.27 [start] => 2020/03/12 [end] => 2020/03/19 )
Array ( [sum] => 2.14 [start] => 2020/03/19 [end] => 2020/03/26 )
Array ( [sum] => 11.82 [start] => 2020/03/26 [end] => 2020/04/02 )
Array ( [sum] => 6.95 [start] => 2020/04/02 [end] => 2020/04/09 )
Array ( [sum] => 36 [start] => 2020/04/09 [end] => 2020/04/16 )
Array ( [sum] => 0.81 [start] => 2020/04/16 [end] => 2020/04/23 )
Array ( [sum] => 30.98 [start] => 2020/04/23 [end] => 2020/04/30 )
Array ( [sum] => 1.3 [start] => 2020/04/30 [end] => 2020/05/07 )
Array ( [sum] => 3.29 [start] => 2020/05/07 [end] => 2020/05/14 )
Array ( [sum] => 1.57 [start] => 2020/05/14 [end] => 2020/05/21 )
Array ( [sum] => 1.95 [start] => 2020/05/21 [end] => 2020/05/28 )
Array ( [sum] => 0.29 [start] => 2020/05/28 [end] => 2020/06/04 )
Array ( [sum] => 2.19 [start] => 2020/06/04 [end] => 2020/06/11 )
Array ( [sum] => 1.57 [start] => 2020/06/11 [end] => 2020/06/18 )
Array ( [sum] => 1.95 [start] => 2020/06/18 [end] => 2020/06/25 )
Array ( [sum] => 1.3 [start] => 2020/06/25 [end] => 2020/07/02 )
Array ( [sum] => 3.29 [start] => 2020/07/02 [end] => 2020/07/09 )
Array ( [sum] => 0.67 [start] => 2020/07/09 [end] => 2020/07/16 )
Array ( [sum] => 0.33 [start] => 2020/07/16 [end] => 2020/07/23 )
Array ( [sum] => 2.73 [start] => 2020/07/23 [end] => 2020/07/30 )
Array ( [sum] => 17.79 [start] => 2020/07/30 [end] => 2020/08/06 )
Array ( [sum] => 1.57 [start] => 2020/08/06 [end] => 2020/08/13 )
Array ( [sum] => 1.95 [start] => 2020/08/13 [end] => 2020/08/20 )
Array ( [sum] => 1.3 [start] => 2020/08/20 [end] => 2020/08/27 )
Array ( [sum] => 3.29 [start] => 2020/08/27 [end] => 2020/09/03 )
Array ( [sum] => 1.3 [start] => 2020/09/03 [end] => 2020/09/10 )
Array ( [sum] => 3.29 [start] => 2020/09/10 [end] => 2020/09/17 )
Array ( [sum] => 1.57 [start] => 2020/09/17 [end] => 2020/09/24 )
Array ( [sum] => 1.95 [start] => 2020/09/24 [end] => 2020/10/01 )
Although I can not figure out how to manipulate it without breaking it...

How to combine multiple array elements with common data?

I have an input array which contains arrays of day and time data:
$temp = Array
(
[0] => Array
(
[day] => Tuesday
[time] => 07:44 pm - 08:44 pm
)
[1] => Array
(
[day] => Tuesday
[time] => 04:00 am - 04:25 am
)
[2] => Array
(
[day] => Sunday
[time] => 04:00 am - 04:25 am
)
[3] => Array
(
[day] => Sunday
[time] => 04:00 am - 04:25 am
)
[4] => Array
(
[day] => Friday
[time] => 04:00 am - 04:25 am
)
[5] => Array
(
[day] => Friday
[time] => 04:00 am - 04:25 am
)
)
Now I want to group the common times display as one element and if the time is the same for more than one day then it's should display one entry. So what is the best way to achieve the desired result without making this too complex?
Array
(
[0] => Array
(
[day] => Tuesday
[time] => 04:00 am - 04:25 am & 07:44 pm - 08:44 pm
)
[1] => Array
(
[day] => Friday & Sunday
[time] => 04:00 am - 04:25 am
)
)
Here it's what I have done:
$final = [];
foreach ($temp as $val) {
$final[strtolower($val['day'])][] = $val['time'];
}
foreach ($final as $k => $v) {
sort($v);
$v = array_unique($v);
$last = array_pop($v);
$final[$k] = [
'day' => $k,
'time' => count($v) ? implode(", ", $v) . " & " . $last : $last,
];
}
There are 4 basic steps:
Remove duplicate rows.
Sort all rows by day (via lookup) ASC, then time (as a string) ASC.
Store joined time values using day values as temporary keys.
Store joined day values using joined time values as temporary keys.
Code: (Demo)
$array=[
['day'=>'Tuesday','time'=>'07:44 pm - 08:44 pm'],
['day'=>'Tuesday','time'=>'04:00 am - 04:25 am'],
['day'=>'Sunday','time'=>'04:00 am - 04:25 am'],
['day'=>'Sunday','time'=>'04:00 am - 04:25 am'],
['day'=>'Friday','time'=>'04:00 am - 04:25 am'],
['day'=>'Friday','time'=>'04:00 am - 04:25 am']
];
$array=array_unique($array,SORT_REGULAR); // remove exact duplicate rows
$order=array_flip(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']); // lookup array
usort($array,function($a,$b)use($order){ // sort by day name ASC then time ASC
if($order[$a['day']]==$order[$b['day']]){
return $a['time']<=>$b['time']; // 2nd sort criteria: time string
}
return $order[$a['day']]<=>$order[$b['day']]; // 1st sort criteria: day name via lookup array
});
foreach($array as $row){
if(!isset($temp[$row['day']])){
$temp[$row['day']]=$row['time']; // store time for first occurring day
}else{
$temp[$row['day']].=" & {$row['time']}"; // join times (for pre-existing day) as they are found
}
}
foreach($temp as $day=>$times){
if(!isset($result[$times])){
$result[$times]=['day'=>$day,'time'=>$times]; // store first occurring day and time using time as temp key
}else{
$result[$times]['day'].=" & $day"; // join day names as they are found
}
}
var_export(array_values($result)); // optionally remove the temporary keys
Output:
array (
0 =>
array (
'day' => 'Tuesday',
'time' => '04:00 am - 04:25 am & 07:44 pm - 08:44 pm',
),
1 =>
array (
'day' => 'Friday & Sunday',
'time' => '04:00 am - 04:25 am',
),
)
Here is another version that doesn't call array_unique(), but I don't like it as much because it does iterated sort() calls and generates a deeper temporary array.
foreach($array as $row){
$temp[$row['day']][$row['time']]=$row['time']; // remove duplicates and group by day
}
$order=array_flip(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']); // lookup array
uksort($temp,function($a,$b)use($order){
return $order[$a]<=>$order[$b]; // sort by day name via lookup array
});
foreach($temp as $day=>$times){
sort($times); // sort time elements ASC
$time_key=implode(' & ',$times); // join the day's time elements
if(!isset($result[$time_key])){ // if first occurrence of the time string
$result[$time_key]=['day'=>$day,'time'=>$time_key]; // store data using time string as temp key
}else{
$result[$time_key]['day'].=" & {$day}"; // concatenate new day to day element
}
}
var_export(array_values($result));

loop through multidimensional array and order sub-array by scores

I am trying to calculate the winning order of golfers when they are tied in a competition.
These golf competitions are using the "stableford" points scoring system, where you score points per hole with the highest points winning. Compared to normal golf "stroke play" where the lowest score wins (though this also has the countback system, only calculating the lowest score in the event of a tie...)
The rules are to use a "countback". i.e., if scores are tied after 9 holes, the best placed of the ties is the best score from the last 8 holes. then 7 holes, etc.
The best I can come up with is 2 arrays.
An array with all the players who tied in a given round. ($ties)
One which has the full score data in (referencing the database playerid) for all 9 holes. ($tie_perhole)
I loop through array 1, pulling data from array 2 and using the following formula to create a temporary array with the highest score:
$max = array_keys($array,max($array));
If $max only has 1 item, this player is the highest scorer. the loop through the first array is "by reference", so on the next iteration of the loop, his playerid is now longer in the array, thus ignored. this continues until there is only 1 playerid left in the first array.
However, it only works if a single player wins in each iteration. The scenario that doesn't work is if a sub-set of players tie on any iterations / countbacks.
I think my problem is the current structure I have will need the original $ties array to become split, and then to continue to iterate through the split arrays in the same way...
As an example...
The $ties array is as follows:
Array
(
[18] => Array
(
[0] => 77
[1] => 79
[2] => 76
[3] => 78
)
)
The $tie_perhole (score data) array is as follows:
Array
(
[18] => Array
(
[77] => Array
(
[9] => 18
[8] => 16
[7] => 14
[6] => 12
[5] => 10
[4] => 8
[3] => 6
[2] => 4
[1] => 2
)
[79] => Array
(
[9] => 18
[8] => 17
[7] => 15
[6] => 14
[5] => 11
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
[76] => Array
(
[9] => 18
[8] => 16
[7] => 14
[6] => 12
[5] => 10
[4] => 8
[3] => 6
[2] => 4
[1] => 2
)
[78] => Array
(
[9] => 18
[8] => 17
[7] => 15
[6] => 13
[5] => 11
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
)
)
So in this competition, player's 78 and 79 score highest on the 8th hole countback (17pts), so 1st and 2nd should be between them. Player 79 should then be 1st on the 6th hole countback (14pts, compared to 13pts). The same should occur for 3rd and 4th place with the 2 remaining other players.
There are other scenarios that can occur here, in that within a competition, there will likely be many groups of players (of different amounts) on different tied points through the leaderboard.
Also note, there will be some players on the leaderboard who are NOT tied and stay in their current outright position.
The basics of the working code I have is:
foreach ($ties as $comparekey => &$compareval) {
$tie_loop = 0;
for ($m = 9; $m >= 1; $m--) {
$compare = array();
foreach ($compareval as $tie) {
$compare[$tie] = $tie_perhole[$comparekey][$tie][$m];
}
$row = array_keys($compare,max($compare));
if (count($row) == 1) {
$indexties = array_search($row[0], $ties[$comparekey]);
unset($ties[$comparekey][$indexties]);
// Now update this "winners" finishing position in a sorted array
// This is a multidimensional array too, with custom function...
$indexresults = searchForId($row[0], $comp_results_arr);
$comp_results_arr[$indexresults][position] = $tie_loop;
$tie_loop++;
}
// I think I need conditions here to filter if a subset of players tie
// Other than count($row) == 1
// And possibly splitting out into multiple $ties arrays for each thread...
if (empty($ties[$comparekey])) {
break;
}
}
}
usort($comp_results_arr, 'compare_posn_asc');
foreach($comp_results_arr as $row) {
//echo an HTML table...
}
Thanks in advance for any helpful insights, tips, thoughts, etc...
Robert Cathay asked for more scenarios. So here is another...
The leaderboard actually has more entrants (player 26 had a bad round...), but the code i need help with is only bothered about the ties within the leaderboard.
Summary leaderboard:
Points Player
21 48
21 75
20 73
20 1
13 26
This example produces a $tie_perhole array of:
Array
(
[21] => Array
(
[75] => Array
(
[9] => 21
[8] => 19
[7] => 16
[6] => 14
[5] => 12
[4] => 9
[3] => 7
[2] => 5
[1] => 3
)
[48] => Array
(
[9] => 21
[8] => 19
[7] => 16
[6] => 13
[5] => 11
[4] => 9
[3] => 8
[2] => 5
[1] => 3
)
)
[20] => Array
(
[73] => Array
(
[9] => 20
[8] => 18
[7] => 16
[6] => 13
[5] => 11
[4] => 8
[3] => 6
[2] => 5
[1] => 3
)
[1] => Array
(
[9] => 20
[8] => 17
[7] => 16
[6] => 14
[5] => 12
[4] => 9
[3] => 7
[2] => 4
[1] => 2
)
)
)
In this example, the array shows that players 75 and 48 scored 21 points that player 75 will eventually win on the 6th hole countback (14pts compared to 13pts) and player 48 is 2nd. In the next tied group, players 73 and 1 scored 20 points, and player 73 will win this group on the 8th hole countback and finishes 3rd (18 pts compared to 17 pts), with player 1 in 4th. player 26 is then 5th.
Note, the $tie_loop is added to another array to calculate the 1st to 5th place finishing positions, so that is working.
Hopefully that is enough to help.
Ok, so I don't understand golf at all... hahaha BUT! I think I got the gist of this problem, so heres my solution.
<?php
/**
* Author : Carlos Alaniz
* Email : Carlos.glvn1993#gmail.com
* Porpuse : Stackoverflow example
* Date : Aug/04/2015
**/
$golfers = [
"A" => [1,5,9,1,1,2,3,4,9],
"B" => [2,6,4,2,4,4,1,9,3],
"C" => [3,4,9,8,1,1,5,1,3],
"D" => [1,5,1,1,1,5,4,5,8]
];
//Iterate over scores.
function get_winners(&$golfers, $hole = 9){
$positions = array(); // The score numer is the key!
foreach ($golfers as $golfer=>$score ) { // Get key and value
$score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
$total_score = (string)array_sum($score_sub); // Get the key
if(!isset($positions[$total_score])){
$positions[$total_score] = array(); // Make array
}
$positions[$total_score][] = $golfer; // Add Golpher to score.
}
ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
return array(end($positions), key($positions)); // The last shall be first
}
//Recursion is Awsome
function getWinner(&$golfers, $hole = 9){
if ($hole == 0) return;
$winner = get_winners($golfers,$hole); // Get all ties, if any.
if(count($winner[0]) > 1){ // If theirs ties, filter again!
$sub_golfers =
array_intersect_key($golfers,
array_flip($winner[0])); // Only the Worthy Shall Pass.
$winner = getWinner($sub_golfers,$hole - 1); // And again...
}
return $winner; // We got a winner, unless they really tie...
}
echo "<pre>";
print_R(getWinner($golfers));
echo "</pre>";
Ok... Now ill explain my method...
Since we need to know the highest score and it might be ties, it makes no sense to me to maintain all that in separate arrays, instead I just reversed the
golfer => scores
to
Tota_score => golfers
That way when we can sort the array by key and obtain all the golfers with the highest score.
Now total_score is the total sum of a subset of the holes scores array. So... the first time this function runs, it will add all 9 holes, in this case theres 3 golfers that end up with the same score.
Array
(
[0] => Array
(
[0] => A
[1] => B
[2] => C
)
[1] => 35
)
Since the total count of golfers is not 1 and we are still in the 9th hole, we run this again, but this time only against those 3 golfers and the current hole - 1, so we are only adding up to the 8th hole this time.
Array
(
[0] => Array
(
[0] => B
[1] => C
)
[1] => 32
)
We had another tie.... this process will continue until we reach the final hole, or a winner.
Array
(
[0] => Array
(
[0] => C
)
[1] => 31
)
EDIT
<?php
/**
* Author : Carlos Alaniz
* Email : Carlos.glvn1993#gmail.com
* Porpuse : Stackoverflow example
**/
$golfers = [
"77" => [2,4,6,8,10,12,14,16,18],
"79" => [3,5,7,9,11,14,15,17,18],
"76" => [2,4,6,8,10,12,14,16,18],
"78" => [3,5,7,9,11,13,15,17,18]
];
//Iterate over scores.
function get_winners(&$golfers, $hole = 9){
$positions = array(); // The score numer is the key!
foreach ($golfers as $golfer => $score) { // Get key and value
//$score_sub = array_slice($score,0,$hole); // Get the scores subset, first iteration is always all holes
$total_score = (string)$score[$hole-1]; // Get the key
if(!isset($positions[$total_score])){
$positions[$total_score] = array(); // Make array
}
$positions[$total_score][] = $golfer; // Add Golpher to score.
}
ksort($positions, SORT_NUMERIC); // Sort based on key, low -> high
return [
"winner"=> end($positions),
"score" => key($positions),
"tiebreaker_hole" => [
"hole"=>$hole,
"score"=> key($positions)],
]; // The last shall be first
}
//Recursion is Awsome
function getWinner(&$golfers, $hole = 9){
if ($hole == 0) return;
$highest = get_winners($golfers,$hole); // Get all ties, if any.
$winner = $highest;
if(count($winner["winner"]) > 1){ // If theirs ties, filter again!
$sub_golfers =
array_intersect_key($golfers,
array_flip($winner["winner"])); // Only the Worthy Shall Pass.
$winner = getWinner($sub_golfers,$hole - 1); // And again...
}
$winner["score"] = $highest["score"];
return $winner; // We got a winner, unless they really tie...
}
echo "<pre>";
print_R(getWinner($golfers));
echo "</pre>";
Result:
Array
(
[winner] => Array
(
[0] => 79
)
[score] => 18
[tiebreaker_hole] => Array
(
[hole] => 6
[score] => 14
)
)

Sorting an array dependent on values [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
Im trying to figure out how to sort the array below so that past array items are sent to the end of the array staying in start_date descending order.
Edit. Id probably include a time array key value item in all arrays to sort by start_date.
[216] => Array (
[title] => Production 1
[start_date] => 20th Feb
[end_date] => 23rd Feb 2015
[ticket_link] => http://www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/3514/1762/4350/Biz-Bio-Pic.jpg
[past] => 1
)
[218] => Array(
[title] => Production 3
[start_date] => 27th Feb
[end_date] => 2nd Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/9414/1762/4351/Dan-Bio-Pic.jpg
[past] => 1
)
[219] => Array (
[title] => Production 4
[start_date] => 3rd Mar
[end_date] => 5th Mar 2015
[ticket_link] => www.google.co.uk
[writer] => Sarah Ruhl
[thumb_image] => /files/4314/1762/4351/Kate-Bio-Pic.jpg
[past] => 0
)
Try this -
function checkdate($a, $b)
{
$a = strtotime($a['start_date']);
$b = strtotime($b['start_date']);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
function checkpast($a, $b)
{
$a_start = strtotime($a['start_date']);
$b_start = strtotime($b['start_date']);
if ($a_start == b_start ) {
return ($a['past'] > $b['past']) ? -1 : 1;
}
}
$array = //your array
usort($array, "checkdate");
usort($array, "checkpast");

Compare todays dayname and time through array?

I have an array like this:
Array
(
[0] => stdClass Object
(
[Start] => 08:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Math
)
[1] => stdClass Object
(
[Start] => 10:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Geography
)
[2] => stdClass Object
(
[Start] => 14:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Science
)
[3] => stdClass Object
(
[Start] => 10:00
[dayName] => Thursday
[dayID] => 4
[courseName] => Math
)
[4] => stdClass Object
(
[Start] => 18:00
[dayName] => Friday
[dayID] => 5
[courseName] => History
)
)
What I want to do is , I want to compare the daya nd time now to the values in the array. For example lets assume that it is 7:00 am and it is Tuesday. Then I want to get the Object[0]. But if it is 11:00 o'clock then i need to get the Object[2] which starts at 14:00 on Tuesday.
It it is Tuesday and 16:00 o'clock then i need Object[3] .
If it is a weekend then i need the beginning of the week which is Tuesday at 08:00 with the Math Course.
I tried to get that using key => value but I mixed up.
How can I compare the Day and then time and in case there is a Course on that combination just pick it up if not just continue checking.
regards
littleblue
function getObject($array){
$timeNow = date('U'); // time now
$oneHour = $timeNow+3600; // time now + 1 hour
foreach($array as $num => $one){ // loop in each $array
$date = strtotime($one->Start); // convert start time to timestamp
if($date >= $timeNow && $date < $oneHour && date('l', $date) == $one->dayName){ // if all criteria met
return $array[$num]; // return that object
}
}
return array('no data'); // if no criteria is met return no data.
}
$course = getObject($yourArray);
echo $course->courseName;
You can mix the use of DateTime and a simple min search :
$myCourse = null;//initialisation
$myCourseDate =null;
$now = new DateTime;
foreach($array as $course){
//get the date from the datas
$date = DateTime::createFromFormat('l h:i',$course->dayName.' '.course->start);
if($now < $date && ($myCourseDate === null || $myCourseDate > $date)){
$myCourse = $course;
$myCourseDate = $date;
}
}
//at the end of the loop, you've got the expected course

Categories