PHP split / explode - php

I currently have some text like this:
W. 50DateWorkSuggested breakTotalMondayTuesday11/1217:00-01:0020:30-21:157:15Wednesday12/1217:00-01:0019:00-19:457:15ThursdayFridaySaturday15/1216:00-23:0020:00-20:456:15Sunday16/1217:00-01:0021:00-21:457:15Total:28W. 51DateWorkSuggested breakTotalMonday17/1217:00-01:0019:00-19:457:15TuesdayWednesday19/1217:00-00:0021:00-21:456:15Thursday20/1223:00-07:0003:00-03:457:15Friday21/1223:00-07:0002:30-03:157:15SaturdaySundayTotal:28W. 52DateWorkSuggested breakTotalMondayTuesdayWednesdayThursdayFridaySaturdaySundayTotal:0W. 1DateWorkSuggested breakTotalMonday------Total:0 Total number of hours: 77:45 Schedule for Month: December
It originally looked different but I have removed the html tags as it was all in tables,
I now however want to spilt / explode this data so that theres one day on each line reading like:
Monday 10/12:
Tuesday 11/12: 17:00 - 01:00
Wednesday 12/12: 17:00 - 01:00
Thursday 13/12:
Friday 14/12:
Saturday: 15/12: 16:00 - 23:00
Thats an example for the first week, I have tried code like:
preg_match_all (("/Monday|Tuesday|Wednesday|Thusday|Fiday|Saturday|Sunday/"), $content, $parts );
print_r ($parts);
But not really having any luck, so any help would be appreciated,
Thanks,
Jack.
ORIGINAL CODE WITH HTML TAGS:
<body> <table border=0 class=blackborder cellpadding=2 cellspacing=2>
<tr><td><table class=blackborder width=315><tr><td><b>W. 50</b></td><td><B>Date</B></td><td><B>Work</B></td><td><B>Suggested break</B></td><td><B>Total</B></td><tr><td></tr><tr><td>Monday</tr><tr><td>Tuesday<td>11/12</td><td>17:00-01:00</td><td>20:30-21:15</td><td>7:15</td></tr><tr><td>Wednesday<td>12/12</td><td>17:00-01:00</td><td>19:00-19:45</td><td>7:15</td></tr><tr><td>Thursday</tr><tr><td>Friday</tr><tr><td>Saturday<td>15/12</td><td>16:00-23:00</td><td>20:00-20:45</td><td>6:15</td></tr><tr><td>Sunday<td>16/12</td><td>17:00-01:00</td><td>21:00-21:45</td><td>7:15</td></td></tr><tr><td>Total:</td><td></td><td></td><td></td><td>28</td></tr></table></td><td><table class=blackborder width=315><tr><td><b>W. 51</b></td><td><B>Date</B></td><td><B>Work</B></td><td><B>Suggested break</B></td><td><B>Total</B></td><tr><td></tr><tr><td>Monday<td>17/12</td><td>17:00-01:00</td><td>19:00-19:45</td><td>7:15</td></tr><tr><td>Tuesday</tr><tr><td>Wednesday<td>19/12</td><td>17:00-00:00</td><td>21:00-21:45</td><td>6:15</td></tr><tr><td>Thursday<td>20/12</td><td>23:00-07:00</td><td>03:00-03:45</td><td>7:15</td></tr><tr><td>Friday<td>21/12</td><td>23:00-07:00</td><td>02:30-03:15</td><td>7:15</td></tr><tr><td>Saturday</tr><tr><td>Sunday</td></tr><tr><td>Total:</td><td></td><td></td><td></td><td>28</td></tr></table></td></tr><tr><td><table class=blackborder width=315><tr><td><b>W. 52</b></td><td><B>Date</B></td><td><B>Work</B></td><td><B>Suggested break</B></td><td><B>Total</B></td><tr><td></tr><tr><td>Monday</tr><tr><td>Tuesday</tr><tr><td>Wednesday</tr><tr><td>Thursday</tr><tr><td>Friday</tr><tr><td>Saturday</tr><tr><td>Sunday</td></tr><tr><td>Total:</td><td></td><td></td><td></td><td>0</td></tr></table></td><td><table class=blackborder width=315><tr><td><b>W. 1</b></td><td><B>Date</B></td><td><B>Work</B></td><td><B>Suggested break</B></td><td><B>Total</B></td><tr><td></tr><tr><td>Monday</td></tr><tr><td>-</td></tr><tr><td>-</td></tr><tr><td>-</td></tr><tr><td>-</td></tr><tr><td>-</td></tr><tr><td>-</td></tr><tr><td>Total:</td><td></td><td></td><td></td><td>0</td></tr></table> </td> </tr> <tr> <td colspan=2> <b>Total number of hours:</b> 77:45<br> </td> </tr> </table> </body>

well i can get a day and time with regex
$str = 'W. 50DateWorkSuggested breakTotalMondayTuesday11/1217:00-01:0020:30-21:157:15Wednesday12/1217:00-01:0019:00-19:457:15ThursdayFridaySaturday15/1216:00-23:0020:00-20:456:15Sunday16/1217:00-01:0021:00-21:457:15Total:28W. 51DateWorkSuggested breakTotalMonday17/1217:00-01:0019:00-19:457:15TuesdayWednesday19/1217:00-00:0021:00-21:456:15Thursday20/1223:00-07:0003:00-03:457:15Friday21/1223:00-07:0002:30-03:157:15SaturdaySundayTotal:28W. 52DateWorkSuggested breakTotalMondayTuesdayWednesdayThursdayFridaySaturdaySundayTotal:0W. 1DateWorkSuggested breakTotalMonday------Total:0 Total number of hours: 77:45 Schedule for Month: December';
$ptr = "/(?P<day>:Monday|Tuesday|Wednesday|Thusday|Fiday|Saturday|Sunday?)(?P<time>([0-9:\/\- ]?)+)/";
preg_match_all($ptr, $str, $data);
echo '<pre>';
print_r($data);
echo '</pre>';
the output will be like this
Array
(
[0] => Array
(
[0] => Tuesday11/1217:00-01:0020:30-21:157:15
[1] => Wednesday12/1217:00-01:0019:00-19:457:15
[2] => Saturday15/1216:00-23:0020:00-20:456:15
[3] => Sunday16/1217:00-01:0021:00-21:457:15
[4] => Tuesday
[5] => Wednesday19/1217:00-00:0021:00-21:456:15
[6] => Saturday
[7] => Sunday
[8] => Tuesday
[9] => Wednesday
[10] => Saturday
[11] => Sunday
)
[day] => Array
(
[0] => Tuesday
[1] => Wednesday
[2] => Saturday
[3] => Sunday
[4] => Tuesday
[5] => Wednesday
[6] => Saturday
[7] => Sunday
[8] => Tuesday
[9] => Wednesday
[10] => Saturday
[11] => Sunday
)
[1] => Array
(
[0] => Tuesday
[1] => Wednesday
[2] => Saturday
[3] => Sunday
[4] => Tuesday
[5] => Wednesday
[6] => Saturday
[7] => Sunday
[8] => Tuesday
[9] => Wednesday
[10] => Saturday
[11] => Sunday
)
[time] => Array
(
[0] => 11/1217:00-01:0020:30-21:157:15
[1] => 12/1217:00-01:0019:00-19:457:15
[2] => 15/1216:00-23:0020:00-20:456:15
[3] => 16/1217:00-01:0021:00-21:457:15
[4] =>
[5] => 19/1217:00-00:0021:00-21:456:15
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
)
[2] => Array
(
[0] => 11/1217:00-01:0020:30-21:157:15
[1] => 12/1217:00-01:0019:00-19:457:15
[2] => 15/1216:00-23:0020:00-20:456:15
[3] => 16/1217:00-01:0021:00-21:457:15
[4] =>
[5] => 19/1217:00-00:0021:00-21:456:15
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
)
)
then you can loop to make a data nice ;)

Related

PHP reorder indexes nested array

I've this array, that i want to reoder indexes of "Mer" elements.
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[3] => 09:00
[4] => 10:00
[5] => 11:00
[6] => 12:00
[7] => 13:00
[8] => 14:00
[9] => 15:00
[10] => 16:00
[11] => 17:00
[12] => 18:00
)
)
)
[Padel 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[0] => 05:00
[1] => 06:00
[2] => 07:00
[3] => 08:00
[4] => 09:00
[5] => 10:00
[6] => 11:00
)
)
)
)
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[3] => 09:00
[4] => 10:00
[5] => 11:00
[6] => 12:00
[7] => 13:00
[8] => 14:00
[9] => 15:00
[10] => 16:00
[11] => 17:00
[12] => 18:00
)
)
)
[Padel 1] => Array
(
[IntervalTime] => Array
(
[Mer] => Array
(
[4] => 09:00
[5] => 10:00
[6] => 11:00
)
)
)
)
This array is back from
unset($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]][$index]);
so i need to reorder this: $DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]], but something was wrong in my concept.
I've tried array_values() for this multidimensional array but that was not good.
Any idea is apprectiated.
You are correct in wanting to use array_values to reorder indexes. Based on your array structure, this should work:
$DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]] = array_values($DisponibilitaRoom[$room]["IntervalTime"][$Giorni[$DayOfWeekGiornoScelto]]);

Unset element in array nested, between a time interval

I'm trying to remove an element.
I've a reservation with:
$StartTime = "14:00"; $EndTime = "16:30";
and Array with
Array
(
[Tennis 1] => Array
(
[IntervalTime] => Array
(
[Lun] => Array
(
[0] => 10:00
[1] => 11:00
[2] => 12:00
[3] => 13:00
[4] => 14:00
[5] => 15:00
[6] => 16:00
[7] => 17:00
[8] => 18:00
[9] => 19:00
[10] => 20:00
)
[Mar] => Array
(
[0] => 12:00
[1] => 13:00
[2] => 14:00
[3] => 15:00
)
[Mer] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Gio] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Ven] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
)
[Sab] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
[5] => 14:00
[6] => 15:00
[7] => 16:00
[8] => 17:00
[9] => 18:00
)
[Sun] => Array
(
[0] => 09:00
[1] => 10:00
[2] => 11:00
[3] => 12:00
[4] => 13:00
[5] => 14:00
[6] => 15:00
[7] => 16:00
[8] => 17:00
[9] => 18:00
)
)
)
I need to remove element between $StartTime and $EndTime
so for "Lun" array need to be removed:
[0] => 10:00
[1] => 11:00
[2] => 12:00
[3] => 13:00
[4] => 14:00 // need remove
[5] => 15:00 // need remove
[6] => 16:00 // need remove
[7] => 17:00
[8] => 18:00
[9] => 19:00
[10] => 20:00
I would to foearch the time from Start to End date, and then remove with:
unset($DisponibilitaRoom[$Nomeroom]["IntervalTime"]["Lun"][$index]);
You correct - simple unset should do it - but you need to loop on the times and remove them as:
foreach($arr['Tennis 1']['IntervalTime']['Lun'] as $k => $v) {
if ($v >= $StartTime && $v < $EndTime)
unset($arr['Tennis 1']['IntervalTime']['Lun'][$k]);
}
Live example: 3v4l
Or you can use array-filter as
$arr['Tennis 1']['IntervalTime']['Lun'] = array_filter($arr['Tennis 1']['IntervalTime']['Lun'], function ($v) use ($StartTime, $EndTime) {
return !($v >= $StartTime && $v < $EndTime);
} );
You can use array_walk_recursive with array_map and array_filter
array_walk_recursive($a['Tennis 1']['IntervalTime'], function(&$v) use ($StartTime, $EndTime){
if($v >= $StartTime && $v <= $EndTime) $v = false;
});
$f['Tennis 1']['IntervalTime'] = array_map('array_filter', $a['Tennis 1']['IntervalTime']);
print_r($f);
Working example :- https://3v4l.org/4tNEM

Grouping consecutive dates in an array together in PHP ignoring weekends and bank holidays

I have an array of dates
$dates = array("2019-04-09","2019-04-17","2019-04-18","2019-04-23","2019-04-24","2019-04-26","2019-04-29","2019-04-30");
what i would like is to pass the $date array through a function and that function would group consecutive working days together to produce the following format.
$newarray = array(
[0]=>array('start' => '2019-04-09','end' => '2019-04-09'),
[1]=>array('start' => '2019-04-17','end' => '2019-04-24'),
[2]=>array('start' => '2019-04-26','end' => '2019-04-30'),
);
Working days are non weekend and non uk bank holidays. I have bank holidays already in another array)
$bankholidays = array(
[0] => 2018-01-01
[1] => 2018-03-30
[2] => 2018-04-02
[3] => 2018-05-07
[4] => 2018-05-28
[5] => 2018-08-27
[6] => 2018-12-25
[7] => 2018-12-26
[8] => 2019-01-01
[9] => 2019-04-19
[10] => 2019-04-22
[11] => 2019-05-06
[12] => 2019-05-27
[13] => 2019-08-26
[14] => 2019-12-25
[15] => 2019-12-26
[16] => 2020-01-01
[17] => 2020-04-10
[18] => 2020-04-13
[19] => 2020-05-04
[20] => 2020-05-25
[21] => 2020-08-31
[22] => 2020-12-25
[23] => 2020-12-28
)
the following function gives me date ranges in the format i require but doesn't take into consideration weekends and bank holidays
function genarray($dates) {
$i=0;
$j=1;
$diff=86400;
$period = $diff;
$nrInterval=0;
$intervals[$nrInterval]['start'] = $dates[$i];
$intervals[$nrInterval]['end'] = $dates[$i];
while($j<count($dates)){
if(strtotime($dates[$j])-strtotime($dates[$i]) == $period){
$intervals[$nrInterval]['end'] = $dates[$j];
$j++;
$period+=$diff;
} else{
$i=$j;
$j++;
$nrInterval++;
$intervals[$nrInterval]['start'] = $dates[$i];
$intervals[$nrInterval]['end'] = $dates[$i];
$period = $diff;
}
}
return $intervals;
}
$dates = array("2019-04-09","2019-04-17","2019-04-18","2019-04-19","2019-04-23","2019-04-24","2019-04-26","2019-04-29","2019-04-30");
$tmp = genarray($dates);
print_r($tmp);
gives me
Array
(
[0] => Array
(
[start] => 2019-04-09
[end] => 2019-04-09
)
[1] => Array
(
[start] => 2019-04-17
[end] => 2019-04-19
)
[2] => Array
(
[start] => 2019-04-23
[end] => 2019-04-24
)
[3] => Array
(
[start] => 2019-04-26
[end] => 2019-04-26
)
[4] => Array
(
[start] => 2019-04-29
[end] => 2019-04-30
)
)
Can anyone help please? I just cant get my head around where to go from here.

Get Range of dates from an array

I'm having the following array of dates :
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
[9] => 2016-02-13
[10] => 2016-02-14
[11] => 2016-02-15
[12] => 2016-03-13
[13] => 2016-03-14
[14] => 2016-03-15
[15] => 2016-03-16
[16] => 2016-03-17
[17] => 2016-03-18
[18] => 2016-03-19
[19] => 2016-04-19
[20] => 2016-04-20
[21] => 2016-04-21
[22] => 2016-04-22
)
How can i get the array dates that form a range. Like
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
The dates in the main array I listed can be of any order. The example I provided all the date ranges can be identifiable. But the raay is dynamic and can have date in random order. All I want to have date ranges out if that array. I'm getting mad of thinking. Please help. Thanks in advance.
You have the following code. Please try this.
$array = Array(0 => '2016-02-01',
1 => '2016-02-02',
2 => '2016-03-19',
3 => '2016-02-04',
4 => '2016-02-05',
5 => '2016-03-18',
6 => '2016-02-07',
7 => '2016-02-08',
8 => '2016-02-09',
9 => '2016-02-13',
10 => '2016-02-14',
11 => '2016-02-15',
12 => '2016-03-13',
13 => '2016-03-14',
14 => '2016-03-15',
15 => '2016-03-16',
16 => '2016-03-17',
17 => '2016-02-06',
18 => '2016-02-03',
19 => '2016-04-19',
20 => '2016-04-20',
21 => '2016-04-21',
22 => '2016-04-22');
//echo "<pre>";print_r($array);
sort($array);
$newDateArray = array();
$i = 0;
$arrayCount = count($array);
foreach($array as $key=>$val) {
//echo $key;
if(isset($array[$key+1])) {
$date1 = $val;
$date2 = $array[$key+1];
$newDateArray[$i][] = $date1;
$datetime1 = date_create($date1);
$datetime2 = date_create($date2);
$interval = date_diff($datetime1, $datetime2);
if($interval->days > 1) {
$i++;
}
}
else if($arrayCount == ($key+1)) {
$newDateArray[$i][] = $date2;
}
}
echo "<pre>";print_r($newDateArray);exit;
your result would be as follows.
Array
(
[0] => Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
[1] => Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
[2] => Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
[3] => Array
(
[0] => 2016-04-19
[1] => 2016-04-20
[2] => 2016-04-21
[3] => 2016-04-22
)
)
Now you can easily split your array. and get the result what you exactly want.
Have a Great Day!!!... Fill free to ask more your doubts...

2 arrays, need the result to be what i show

This is my arrays, i want this result below, but cant figure it out.
The result is a count of how many times an ID have a date between 2014-06-01 to the last day of month 2014-06-xx
In my array with dates "array[1]", only 6-7-8 have wrong dates.
Help please :-S
RESULT
Array
(
[0] => Array
(
[1] => 2
[4] => 2
[7] => 3
[9] => 1
[12] => 1
[13] => 1
)
)
Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
Hope the following works. Did not test it.
$idsArray = $yourArray[0];
$dateArray = $yourArray[1];
$countArray = array(); // the result array
$beginningTimestamp = strtotime('2014-06-01');
$lastTimestamp = strtotime('2014-07-01'); // before the first of July comes the last of June
foreach ($idsArray as $key => $id) {
if (isset($dateArray[$key])) {
$timestamp = strtotime($dateArray[$key];
if ($timestamp >= $beginningTimestamp && $timestamp < $lastTimestamp) {
if (isset($countArray[$id])) {
$countArray[$id]++;
} else {
$countArray[$id] = 1;
}
}
}
}

Categories