I have an array that is produced from people wanting to reserve a time block to volunteer with our organization. I want to check to see if they chose time blocks on the same day that overlap. In my example array below the first and third elements are overlapping and I need to detect that. Any recommendation would be much appreciated:
Array
(
[0] => Array
(
[id_pc_time_blocks] => 3
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 9:00 AM-1:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[1] => Array
(
[id_pc_time_blocks] => 4
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 1:00 PM-5:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 10
[pc_name] => Atlanta
)
[2] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 10:00 AM-2:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[3] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-23
[pc_time_block] => 10:00 AM-2:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[4] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-23
[pc_time_block] => 3:00 AM-6:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
)
Not recommended for HUGE arrays, but here's a quick solution. You need to break hte times into unix time stamps for comparisson
// Run down each element of the array. (I've called it MyStartArray)
$numElements = count($MyStartArray);
for ($i=0; $i<$numElements ; $i++) {
// Calculate "Start Time" and "End Time" as Unix time stamps (use mktime function) and store as another items in the array
// You can use preg_match or substr to get the values to pump into mktime() below - not writing hte whole thing for you ;)
$MyStartArray[$i]['start_time'] = mktime( ... );
$MyStartArray[$i]['end_time'] = mktime( ... );
// Now run through all the previous elements to see if a start time is before the end time, or an end time is after the start time.
if ($i > 0) {
for ($j=0; $j<$i;$j++) {
if ($MyStartArray[$i]['start_time'] < $MyStartArray[$j]['end_time'] ||
$MyStartArray[$j]['end_time'] > $MyStartArray[$j]['start_time'] ) {
echo 'CLASH';
}
}
}
}
Here is my solution for checking each date for overlapping times. The only thing is, this is for my particular scenario and does not account for overlapping years as I dont have that need for this application.
Here is my working example:
$dateIdx = 0;
foreach($timeblocks_array as $obj) {
$timeblocks_array[$dateIdx]["intDay"] = idate("z",strtotime($obj["pc_date"]));
$timeblocks_array[$dateIdx]["intStart"] = intval($obj["start_time"]);
$timeblocks_array[$dateIdx]["intEnd"] = intval($obj["end_time"]);
$mindates[] = idate("z",strtotime($obj["pc_date"]));
$dateIdx++;
}
$minDateSingle = min($mindates);
$maxDateSingle = max($mindates);
$currentDate = $minDateSingle;
$dateIdx = 0;
while ($currentDate <= $maxDateSingle) {
$hrIndex = 0;
while ($hrIndex < 24) {
$matrixArray[$dateIdx][$hrIndex]["count"] = 0;
$matrixArray[$dateIdx][$hrIndex]["intDay"] = $currentDate;
$hrIndex++;
}
// calculate counts:
$hourIdx = 0;
foreach($matrixArray[$dateIdx] as $hour){
foreach($timeblocks_array as $block) {
if ($hour["intDay"] == $block["intDay"]) {
if ($hourIdx >= $block["intStart"] && $hourIdx < $block["intEnd"]) {
$matrixArray[$dateIdx][$hourIdx]["count"] = $matrixArray[$dateIdx][$hourIdx]["count"] + 1;
$matrixArray[$dateIdx][$hourIdx]["requests"][] = $block;
}
}
}
$hourIdx++;
}
$dateIdx++;
$currentDate = $currentDate + 1;
}
//loop through the matrix array and timeblocks array to see if they intersect
foreach($matrixArray as $day) {
$hourIdx = 0;
foreach($day as $hour) {
if ($hour["count"] > 1) {
//echo $hour["intDay"]." - Overlap on Hour $hourIdx\n";
$smarty->assign('overlappingError', 1);
$error = 1;
foreach($hour["requests"] as $overlapblock) {
//echo " --> conflict: ". $overlapblock["pc_date"]." ".$overlapblock["pc_time_block"]." (".$overlapblock["intStart"]." to ".$overlapblock["intEnd"].")\n";
}
} else if ($hour["count"] == 1) {
// these are valid hours
}
$hourIdx++;
}
}
Robbie's answer did not work for me. I found the rules that needed to be in place going off his example for just dates were:
$i[start] needs to be less than and not equal to $i[stop]
$i[start] needs to greater than and not equal to $j[stop]
$j[start] needs to be less than and not equal to $j[stop]
Therefore my solution was:
$numElements = count($dates);
for ($i=0; $i<$numElements; $i++) {
$dates[$i]['start_time'] = strtotime($dates[$i]['start']);
$dates[$i]['end_time'] = strtotime($dates[$i]['end']);
if ($i > 0) {
for ($j=0; $j<$i;$j++) {
if($dates[$i]['start_time'] >= $dates[$i]['end_time'] || $dates[$i]['start_time'] <= $dates[$j]['end_time'] || $dates[$j]['start_time'] >= $dates[$j]['end_time']) {
$this->set_error(['dates_overlap']);
$this->dates_overlap = true;
break;
}
}
}
if(isset($this->dates_overlap))
break;
}
Related
This question already has answers here:
How to get closest date compared to an array of dates in PHP
(6 answers)
Closed 3 years ago.
I have an array called $timeslots with timeslots like:
array:32 [▼
0 => "2018-12-15T12:00:00.0000000"
1 => "2018-12-15T12:15:00.0000000"
2 => "2018-12-15T12:30:00.0000000"
3 => "2018-12-15T12:45:00.0000000"
4 => "2018-12-15T13:00:00.0000000"
5 => "2018-12-15T13:15:00.0000000"
6 => "2018-12-15T13:45:00.0000000"
7 => "2018-12-15T14:15:00.0000000"
8 => "2018-12-15T14:30:00.0000000"
9 => "2018-12-15T14:45:00.0000000"
10 => "2018-12-15T15:00:00.0000000"
11 => "2018-12-15T15:15:00.0000000"
12 => "2018-12-15T15:30:00.0000000"
13 => "2018-12-15T15:45:00.0000000"
14 => "2018-12-15T16:15:00.0000000"
15 => "2018-12-15T16:45:00.0000000"
16 => "2018-12-15T17:00:00.0000000"
17 => "2018-12-15T17:30:00.0000000"
18 => "2018-12-15T17:45:00.0000000"
19 => "2018-12-15T18:30:00.0000000"
20 => "2018-12-15T18:45:00.0000000"
21 => "2018-12-15T19:15:00.0000000"
22 => "2018-12-15T19:45:00.0000000"
23 => "2018-12-15T20:15:00.0000000"
24 => "2018-12-15T20:45:00.0000000"
25 => "2018-12-15T21:00:00.0000000"
26 => "2018-12-15T21:15:00.0000000"
27 => "2018-12-15T21:30:00.0000000"
28 => "2018-12-15T21:45:00.0000000"
29 => "2018-12-15T22:00:00.0000000"
30 => "2018-12-15T22:15:00.0000000"
31 => "2018-12-15T22:30:00.0000000"
]
Also, I have a variable like:
$expected_time = 2018-12-15T18:00:00.0000000; // this can be different value, so its not unique value
$expected_time is never into array $timeslots but I need to find closest value to $expected_time... How I can do that?
How I can get the closest timeslot value from array $timeslots to $expected_time and calculate the difference in minutes?
Any idea?
As Nico mentioned in the comments, it's pretty straightforward. Just looping and calculating the time difference.
$timeslots = [...];
$expected_time = "2018-12-15T18:00:00.0000000";
$timestamp = strtotime($expected_time);
$diff = null;
$index = null;
foreach ($timeslots as $key => $time) {
$currDiff = abs($timestamp - strtotime($time));
if (is_null($diff) || $currDiff < $diff) {
$index = $key;
$diff = $currDiff;
}
}
echo $timeslots[$index];
Here is one solution fitting your requirements:
function findClosestDate($expectedDate,$dates)
{
$differenceInMinutes = null;
$expectedDate = new DateTime($expectedDate);
$expectedDateEpoch = $expectedDate->getTimestamp();
$returnIndex = -1;
for($i = 0; $i<count($dates); $i++)
{
$dateObject = new DateTime($dates[$i]);
$dateEpoch = $dateObject->getTimestamp();
$difference = abs($expectedDateEpoch-$dateEpoch);
$difference = $difference/60;
if($differenceInMinutes === null || $difference < $differenceInMinutes)
{
$differenceInMinutes = $difference;
$returnIndex = $i;
}
}
return array(
"closest" => $dates[$returnIndex],
"difference" => $differenceInMinutes
) ;
}
This makes use of the DateTime class to create a DateTime object and get the respective timestamp. The minutes are then calculated by the absolute difference between the expectedDate and the entry in the dates array. After iterating over the whole array, the closest match and the difference is returned in one array.
as your list is already sorted - u can put the element into array and use sort again - and it will be much faster then calculating the diff in each iteration.
<?php
$timeslots = [
...
];
$expected_time = "2018-12-15T18:00:00.0000000";
$counter = count($timeslots);
$timeslots = array_flip($timeslots);
$timeslots[$expected_time] = $counter;
ksort($timeslots);
while (key($timeslots) !== $expected_time) {
$prev = key($timeslots);
next($timeslots);
}
next($timeslots);
$next = key($timeslots);
$expected_time = new \DateTime($expected_time);
$closestDiff = min(($expected_time)->diff(new \DateTime($prev)), (new \DateTime($next))->diff($expected_time));
var_dump($closestDiff->i);
By given 2 range of year, current population and target population, how to get total population every year by using PHP?
Example:
Year | Population
2014 | 100000
2018 | 132000
On paper calculation like this:
132000 / 100000 = 1.0718
so we will get result every year (On paper):
2014 = 100000
2015 = 107187 (100000 * 1.0718)
2016 = 114890 (107187 * 1.0718)
2017 = 123147 (114890 * 1.0718)
2018 = 132000
How to hold previous year variable to get result as above?
This is my PHP code:
for($i > $start; $i < $end; $i++) {
$this->ProjectPopulation->create();
$increase = array(
'project_id' => $project_id,
'year' => $i,
'percent_increase' => $this->percentage_increase($current_population, $target_population, $year),
'population' => ??? // Problem here
);
$this->ProjectPopulation->save($increase);
}
Thanks
There are two resources I'd recommend you taking a look at:
http://php.net/manual/en/ref.math.php
and
http://www.w3schools.com/php/php_ref_math.asp
You can solve this by doing the calculation each time, but it would be faster to use a loop in order to iterate through them all.
So something like this:
$population = 100000;
$year = 2014;
//I'd print the first one out prior to the loop, or you could put an if($year == 2014) in the loop, your choice really.
for(i= 0; i < 4; i++)
{
//do calculation in here.
echo "The current years is" . $year;
$yearsCalculation = $population * 1.0718;
//then save that value for the next time around the loop.
$population = $yearsCalculation ;
echo $yearsCalculation ;
//increase years each time round loop
$year++;
}
If you then want to store it in a MySQL database you might want to have a look at this tutorial:
http://www.w3schools.com/php/php_mysql_insert.asp
Hope that helped!
Solve
private function power($current_population, $target_population, $year) {
$devide = $target_population / $current_population;
$pwrdevider = 1 / $year;
$power = pow($devide, $pwrdevider);
return $power;
}
private function percentage_increase($current_population, $target_population, $year) {
$devide = $target_population / $current_population;
$power = pow($devide, 1 / $year);
$increase = ($power - 1) * 100;
return $increase;
}
$start = 2014;
$end = 2018;
$diff = $end - $start;
for($i = 1, $data = 100000; $i <= $diff; $i++) {
$data *= $this->power(100000, 132000, $diff);
$increase = array(
'project_id' => 1,
'year' => $start += 1,
'percent_increase' => $this->percentage_increase(100000, 132000, $diff),
'population' => $data
);
print_r($increase);
}
This is my result
Array
(
[project_id] => 1
[year] => 2015
[percent_increase] => 7.1873373728262
[population] => 107187.33737283
)
Array
(
[project_id] => 1
[year] => 2016
[percent_increase] => 7.1873373728262
[population] => 114891.25293076
)
Array
(
[project_id] => 1
[year] => 2017
[percent_increase] => 7.1873373728262
[population] => 123148.87489076
)
Array
(
[project_id] => 1
[year] => 2018
[percent_increase] => 7.1873373728262
[population] => 132000
)
Thanks all
I was wondering if there is a better solution to loop through an array from mid to end then from start to mid. Particularly for an associative array.
So for example if there is an associative array with the keys
$dow = array(Mon => etc, Tue => etc, Wed => etc, Thr => etc .. to .. Sun => etc).
I would start searching the array from Thurs to find the next day with something specific which could be anyday but happens to be on Tues, I usually iterate from Thurs (by index) to Sunday then, reset and start again from Monday to Wed and find the target when reaching Tues.
I count the index via an id and when it reaches 6 reset the id to 0
$id = 3 // Found day is Thursday id
//Loop function starts here
$id++; // start search from one day above found day
if ($id >= 6){ //when reaching Sunday
$id = 0 // start search from monday
}
// check array here for that specific thing
So the question is to ask if there is a more simple solution than this, ie split array from index thursday to sunday and add it onto the beginning of the array and then do the loop without having to count an index or if there are any other solutions without using the count index.
You can try with array_splice:
$array = array(1,2,3,4,5,6,7);
$lastDays = array_splice($array, 3);
$firstDays = $array;
print_r(array('first days' => $firstDays, 'last days' => $lastDays));
If the day is not in $lastDays (use a boolean like $matchFound) then you would search in $firstDays.
Or just use with array_merge:
$array = array(1,2,3,4,5,6,7);
$array = array_merge(array_splice($array, 3), $array);
print_r($array);
where output is:
Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
[4] => 1
[5] => 2
[6] => 3
)
and then you can search with a foreach.
maybe a foreach would be more efficient. hope this helps.
$id = 3 // Found day is Thursday id
$i =0;
//Loop function starts here
foreach($dow as $idx)
if($id == $i):
else if($i > $id):
endif;
if($i == count($dow)){$i=0;}else{$i++;}
endforeach;
<?php
$dow = array("mon","tue","wed","thu","fri","sat","sun");
$pick_a_day = 2; // user input; NON zero based. monday = 1
$pick_a_day--; // make it zero based to use in code
foreach($dow as $inc => $an_element )
{
echo $dow[($pick_a_day+(count($dow))) % (count($dow))]." - loop number:".($inc+1)."\n";
$pick_a_day++;
}
?>
output
tue - loop number:1
wed - loop number:2
thu - loop number:3
fri - loop number:4
sat - loop number:5
sun - loop number:6
mon - loop number:7
Maybe not the best solution, but a solution:
function half($array) {
$h = sizeof($array) / 2;
$a1 = array_splice($array, $h);
$a2 = array_splice($array, 0);
$res = array_merge($a1, $a2);
return $res;
}
$dow = array('Mon' => 'etc1', 'Tue' => 'etc2', 'Wed' => 'etc3', 'Thr' => 'etc4', 'Fri' => 'etc5', 'Sat' => 'etc6', 'Sun' => 'etc7');
$b = half($dow);
Now you can go through with a foreach, or how would you like. Result of $b:
Array
(
[Thr] => etc4
[Fri] => etc5
[Sat] => etc6
[Sun] => etc7
[Mon] => etc1
[Tue] => etc2
[Wed] => etc3
)
I'm really stuck into this situation.
I have these 2 tables:
employee_working_schedule (stores the start and end time an employee works in a specific date)
employee_appointments
Let's suppose we have those rows stored
employee_working_schedule:
start | end
10:00 | 18:00
employee_appointments:
start | end
10:10 | 11:00
11:20 | 12:00
14:30 | 15:20
in this case I want to show that available times are:
10:00 | 10:10
11:00 | 11:20
12:00 | 14:30
15:20 | 18:00
Is there a way of doing this by SQL? I have tried to achieve with php, but no success so far.
Any help will be appreciated.
Here is a way to do it in pure PHP:
class TimeSpan {
function __construct($start, $end) {
$this->start = $start;
$this->end = $end;
}
function starttime() {
list($hour, $minute) = explode(":", $this->start);
return (int)$hour * 60 + (int)$minute;
}
function endtime() {
list($hour, $minute) = explode(":", $this->end);
return (int)$hour * 60 + (int)$minute;
}
}
function convert_to_time($minutes) {
$hour = (int) ($minutes / 60);
$minutes = $minutes % 60;
return str_pad($hour, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT);
}
function open_times($shift, $appointments) {
$alltimes = array_fill_keys(range($shift->starttime(), $shift->endtime()), 1);
foreach ($appointments as $appt) {
$alltimes = array_diff_key($alltimes, array_fill_keys(range($appt->starttime() + 1, $appt->endtime() - 1), 1));
}
$groups = array();
$active_group = 0;
$output = array();
$output_counter = 0;
$nums = array_keys($alltimes);
foreach( $nums as $k => $num ) {
if( $k !== 0 && $nums[$k] !== $nums[$k-1]+1 ) $active_group ++;
$groups[ $active_group ][] = $num;
}
foreach( $groups as $group ) {
$first = array_shift( array_values($group) );
$output[$output_counter][] = $first;
$last = array_pop( array_values($group) );
if( $first !== $last )
$output[$output_counter][] = $last;
$output_counter++;
}
foreach ($output as &$span) {
$span[0] = convert_to_time($span[0]);
$span[1] = convert_to_time($span[1]);
}
return $output;
}
$shift = new TimeSpan("10:00", "18:00");
$appointments = array(
new TimeSpan("10:10", "11:00"),
new TimeSpan("11:20", "12:00"),
new TimeSpan("14:30", "15:20"),
);
print_r(open_times($shift, $appointments));
OUTPUT
Array
(
[0] => Array
(
[0] => 10:00
[1] => 10:10
)
[1] => Array
(
[0] => 11:00
[1] => 11:20
)
[2] => Array
(
[0] => 12:00
[1] => 14:30
)
[3] => Array
(
[0] => 15:20
[1] => 18:00
)
)
I can give you a solution in PHP but it would be, of course, faster if you could find something in SQL
//given you selected $employee_working_schedule
$dayStart = DateTime::createFromFormat('h:i',$employee_working_schedule['start']);
$dayEnd = DateTime::createFromFormat('h:i',$employee_working_schedule['end']);
//then assuming your query statement is named $timeQuery and use PDO
$nextFreeStart = $dayStart;
$newFreeEnd = null;
$freeTime = array();//here we will store the free time intervals
while($times = $timeQuery->fetch(PDO::FETCH_ASSOC)){
$nextFreeEnd = DateTime::createFromFormat('h:i',$times['start']);
$freeTime[] = $nextFreeEnd->diff($nextFreeStart);
$nextFreeStart = DateTime::createFromFormat('h:i',$times['end']);
}
$freeTime[] = $dayEnd->diff($nextFreeStart); //close the day
Strategy: Collect all startpoints, find its respective endpoint for each, then throw away rows that don't comply.
This the idea written in MySQL (untested):
SELECT eas.s AS s_start, MIN(eas.e) AS e_end
FROM ((
SELECT end AS s, start AS e
FROM employee_appointments
) UNION (
SELECT start AS s,end AS e
FROM employee_working_schedule
)) eas
WHERE end > start -- or >= if you want zero-time slots as well
GROUP BY eas.s
HAVING NOT EXISTS ( -- if appointments are disjoint, this is likely redundant
SELECT 0
FROM employee_appointments kill
WHERE s_start > kill.end AND kill.start > e_end
)
I have a scenario where a user submits a start and stop date for a reservation in a hotel. The database has different (daily) prices for periods of the year. Given the user's date range I want to start by counting the number of days the user's range has in each range from the database and then multiplying that by the daily price for that date range.
User's dates:
03 Jun 2012 - 03 Jul 2012
Relevant date ranges from the DB
Array
(
[0] => stdClass Object
(
[date_from] => 2012-04-09
[date_to] => 2012-06-04
[price] => 44
)
[1] => stdClass Object
(
[date_from] => 2012-06-04
[date_to] => 2012-07-02
[price] => 52
)
[2] => stdClass Object
(
[date_from] => 2012-07-02
[date_to] => 2012-07-16
[price] => 61
)
)
As you can see the reservation begins in the first range, spans the second range and ends in the third range.
foreach ($dates as $key => $d)
{
$days = 0;
$user_start = strtotime($range['start']);
$user_stop = strtotime($range['stop']);
$db_start = strtotime($d->date_from);
$db_stop = strtotime($d->date_to);
if( $user_start >= $db_start && $user_stop < $db_stop )
{
$start = $range['start'];
$stop = $range['stop'];
}
elseif( $user_start <= $db_start && $user_stop > $db_start )
{
$start = $d->date_from;
$stop = $d->date_to;
}
elseif( $user_start <= $db_stop && $user_stop > $db_stop )
{
$start = $range['start'];
$stop = $d->date_to;
}
$days = calculate_nbr_days($start, $stop);
$price += $days * $d->price;
}
This code almost works, except for the fact it takes the end of the reservation ($stop) as being the last date of the DB range instead of the user's range and I can't figure out why!
Have you considered doing it all in SQL? Something this should work:
SELECT
SUM(
datediff(
IF($dateTo>date_to,date_to,$dateTo),
IF($dateFrom<date_from,date_from,$dateFrom)
) * price_per_day
) as total_cost
FROM ranges
WHERE '$dateFrom'<=date_to
AND '$dateTo'>=date_from