php - from array output date from monday to friday
Solution:
You could just create a loop, strip out the unwanted items, and build a new array .. Probably the easiest like so ..
<?php
$old_array = array();
$old_array[0] = "Mi 06.01.";
$old_array[1] = "Do 06.01.";
$old_array[2] = "Fr 06.01.";
$old_array[3] = "Mi 06.01.";
$old_array[4] = "Mo 06.01.";
$new_array = array();
foreach ($old_array as $item){
if (strpos($item, 'Mo') !== false || // Checks if $item conains "Mo"
strpos($item, 'Mi') !== false) { // Checks if $item conains "Mi"
// push to new array
array_push($new_array ,$item);
}
}
print_r($new_array);
$new_array output:
Array
(
[0] => Mi 06.01.
[1] => Mi 06.01.
[2] => Mo 06.01.
)
It will be slightly labor intensive -- But really it's the easiest way to accomplish it. To get your "range" you can create a function with a "start" and "end" and compare that way, and return your range as a string or array etc etc ..
<?php
$old_array = array();
$old_array[0]= "Mi 06.01.";
$old_array[1]= "Do 07.01.";
$old_array[2]= "Fr 08.01.";
$old_array[3]= "Mo 11.01.";
$old_array[4]= "Di 12.01.";
$old_array[5]= "Mi 13.01.";
$old_array[6]= "Do 14.01.";
$old_array[7]= "Fr 15.01.";
$old_array[8]= "Mo 18.01.";
$old_array[9]= "Di 19.01.";
$old_array[10]= "Mi 20.01.";
$old_array[11]= "Do 21.01.";
$old_array[12]= "Fr 22.01.";
$old_array[13]= "Mo 25.01.";
$old_array[14]= "Di 26.01.";
$old_array[15]= "Mi 27.01.";
$old_array[16]= "Do 28.01.";
$old_array[17]= "Fr 29.01.";
$start_day = "Mi 06.01.";
$end_day = "Fr 08.01.";
$new_array = makeNewArray($old_array, $start_day, $end_day);
print_r( $new_array );
function makeNewArray($old_array, $start_day, $end_day){
$new_array = array();
foreach ($old_array as $item){
if ($start_day == $item){
$new_array['start'] = $item;
}
if ($end_day == $item){
$new_array['end'] = $item;
}
}
return $new_array;
}
Output:
Array
(
[start] => Mi 06.01.
[end] => Fr 08.01.
)
Start = Mi 06.01.
End = Fr 08.01.
Answer
Solution:
If I understand correctly, you're looking to automatically create the range strings. This should do it. There's not much info provided, so I'm making some assumptions, e.g. the dates are already in the correct order.
Output:
array(4) {
[0]=>
string(21) "Mi 06.01. - Fr 08.01."
[1]=>
string(21) "Mo 11.01. - Fr 15.01."
[2]=>
string(21) "Mo 18.01. - Fr 22.01."
[3]=>
string(21) "Mo 25.01. - Fr 29.01."
}
Explanations inline:
$date_ranges = [];
$current_week_string = false;
foreach ($dates as $date) {
// unsure how to easily parse the german day abbreviations so i'll ignore them
// with ?? and use the dates instead
$datetime = DateTime::createFromFormat('?? d.m.', $date);
// year-week e.g. 2021-01, to keep track of when we change to the next week
$week_string = $datetime->format('Y-W');
// still in the same week as the previous date. skip for now
if ($current_week_string == $week_string) {
$previous_date = $date;
continue;
}
// create the date range (unless this is the first iteration)
if ($current_week_string !== false) {
$date_ranges[] = $start_of_week . ' - ' . $previous_date;
}
// reset everything for the next loop
$start_of_week = $date;
$current_week_string = $week_string;
$previous_date = $date;
}
// create the range for the final date
$date_ranges[] = $start_of_week . ' - ' . $date;
var_dump($date_ranges);
Source