Get the 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, andreturn 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.
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);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.