0

I try the following:
Compare today's date in the array.
Count how many are on or off.
And list them.
And find out which ones are online or offline.

$dateArray = [];
$dateArray = [
  'hercules' => ['date' => ['start' => '2019-10-13','end' => '2021-01-01']], // on
  'serto' => ['date' => ['start' => '2019-12-11','end' => '2019-12-20']], // off
  'alex' => ['date' => ['start' => '2019-08-20','end' => '2023-01-05']], // on
  'herbert' => ['date' => ['start' => '2020-11-11','end' => '2021-09-17']], // off
  'wolfy' => ['date' => ['start' => '2019-09-04','end' => '2021-01-01']], // on
  'susi' => ['date' => ['start' => '2021-05-10','end' => '2022-01-01']] // off
];

$currentDate = date("Y-m-d");
// check date for on
foreach($dateArray as $date){
  if ($date['date']['start'] <= $actualDate  &&  (empty($date['date']['end']) || $date['date']['end'] >= $actualDate )){
     $on = $date;
     $countOn = count($on);
  }
}
echo 'on ('.$countOn.') is: ';
foreach ($on as $pers){
 echo $pers.', ';
};
echo '<br>';

// check date for off
foreach($dateArray as $date){
  if ($date['date']['start'] <= $actualDate  &&  (empty($date['date']['end']) || $date['date']['end'] <= $actualDate )){
     $off = $date;
     $countOff = count($off);
  }
}
echo 'off ('.$countOff.') is: ';
foreach ($off as $pers){
 echo $pers.', ';
};
echo '<br>';

// message
echo 'hello, '.$pers['herbert'].' is '.$onORoff.' and '.$pers['alex'].' is '.$onORoff.'!';

expected

on (3) is: hercules, alex, wolfy
off (3) is: serto, herbert, susi
hello, herbert is off and alex is on!

5
  • It looks like you are asking people to code this for you rather than asking a specific question. Commented Jan 4, 2020 at 14:42
  • don't you see the code? Commented Jan 4, 2020 at 14:50
  • @cnizzardini I think he made a good effort in creating the code himself Commented Jan 4, 2020 at 14:55
  • I haven't looked much at your code but it looks like you are overwriting your variables in the loop. And you have actualdate which is not defined Commented Jan 4, 2020 at 15:00
  • Welcome to StackOverflow. Please follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find online, make an honest solution attempt, and run into a problem, you'd have a good example to post. Commented Jan 4, 2020 at 15:49

1 Answer 1

1

You can loop the array and create a new array that holds the on or off.
Then it's just a matter of intersecting with 'on' and returning the keys to get who is on or off.

$on =0;
$off=0;

foreach($dateArray as $name => $values){
    if(time() >= strtotime($values['date']['start'])  && time() < strtotime($values['date']['end'])){
        $result[$name] = "on";
        $on++;
    }else{
        $result[$name] = "off";
        $off++;
    }
}



echo "on (" . $on . ") is: " . implode(", ", array_keys(array_intersect($result, ['on']))) . "<br>\n";
echo "off (" . $off . ") is: " . implode(", ", array_keys(array_intersect($result, ['off']))) . "<br>\n";
echo "hello, Herbert is " . $result['herbert'] . " and alex is ". $result['alex'];

https://3v4l.org/ghWb2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.