php - Nested foreach increasing both indexes / check if date is between 2 arrays
Get the solution ↓↓↓Is there a way to increase both foreach loops at the same time?
I have 2 arrays:
$datum = new DateTime("now");
$jaar = date("y",strtotime('now'));
$maand = strftime("%B");
$vakanties_start = array(
"BeginKerst" => new DateTime("$jaar-01-01"),
"Voorjaar" => new DateTime("$jaar-02-22"),
"Mei" => new DateTime("$jaar-04-25"),
"Zomer" => new DateTime("$jaar-07-18"),
"EindKerst" => new DateTime("$jaar-12-21"),
"Test" => new DateTime("$jaar-05-20")
);
$vakanties_eind = array(
"BeginKerst" => new DateTime("$jaar-01-05"),
"Voorjaar" => new DateTime("$jaar-03-01"),
"Mei" => new DateTime("$jaar-05-03"),
"Zomer" => new DateTime("$jaar-08-30"),
"EindKerst" => new DateTime("$jaar-12-31"),
"Test" => new DateTime("$jaar-05-25")
);
beginning of the vacation and the ending of vacations. I need to check if today is between those days.
I've tried to check that with 2 foreach loops but than i will get this error: start vacation: 1-1 end vacation: 5-1
start: 1-1 end: 3-1
etc. etc. this obviously isn't the way I want it since that means at the end of the first foreach the holidays are between 1-1 and 31-12.
something else I tried is validating the key of the array like this:
foreach ($vakanties_start as $vakantie_start){
foreach ($vakanties_eind as $vakantie_eind){
if ($vakantie_start->format('Y-m-d') >= $datum->format('Y-m-d') && $vakantie_eind->format('Y-m-d') <= $datum->format('Y-m-d')){
if (key($vakantie_start) == key($vakantie_eind)){
echo $vakantie_start->format('Y-m-d') . "<br>" . key($vakanties_start) . "<br>";
echo $vakantie_eind->format('Y-m-d') . "<br>" . key($vakanties_eind) . "<br>";
}
}
}
}
but this makes the code only show the dates 1-1 to 5-1. can somebody direct me to the right direction? maybe I'm doing it completely wrong but I don't know how else to do it
Answer
Solution:
You were on the right track. I changed how you organize your data so iterating through them is simpler.
I also usedDateTime()
objects in my comparison because they are comparable. No need to format them as a string first.
$jaar = date('Y');
$vakanties = [
'BeginKerst' => [
'start' => new DateTime("$jaar-01-01"),
'end' => new DateTime("$jaar-01-05"),
],
'Voorjaar' => [
'start' => new DateTime("$jaar-02-22"),
'end' => new DateTime("$jaar-03-01"),
],
'Mei' => [
'start' => new DateTime("$jaar-04-25"),
'end' => new DateTime("$jaar-05-03"),
],
'Zomer' => [
'start' => new DateTime("$jaar-07-18"),
'end' => new DateTime("$jaar-08-30"),
],
'EindKerst' => [
'start' => new DateTime("$jaar-12-21"),
'end' => new DateTime("$jaar-12-31"),
],
'Test' => [
'start' => new DateTime("$jaar-05-20"),
'end' => new DateTime("$jaar-05-25"),
],
];
$now = new DateTime();
foreach ($vakanties as $name => $vakanty) {
if ($now >= $vakanty['start'] && $now <= $vakanty['end']) {
echo "today is in the middle of a vacation: " . $name;
}
}
Output
today is in the middle of a vacation: Test
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the payload is invalid.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.