0

I want to compare two DateTime from JSON data with the current DateTime. compare the start date & end date with the current date to data between these two dates. If dates do not match give 0 answer.



$json = '[ 
  {
     "type":"playlist",
     "id": "35",
      "start_datetime": "2022-09-28 09:48",
      "end_datetime": "2022-09-28 09:51"
  },
  {
     "type":"asset",
     "id": "4",
      "start_datetime": "2022-09-29 07:00",
      "end_datetime": "2022-09-29 07:30"
  },
  {
     "type":"asset",
     "id": "4",
      "start_datetime": "2022-09-29 09:00",
      "end_datetime": "2022-09-29 09:30"
  }
]';

function find_events($events, $date) {
    $date = new DateTime($date);
    foreach ($events as $event) {
      $from = (new DateTime($event['start_datetime']));
      $to = (new DateTime($event['end_datetime']));
      if ($date >= $from || $date <= $to) {
           $r= "{$event['start_datetime']} to {$event['end_datetime']}".'<br>';
        }else{
          $r=0;
        }
    }
      return $r;
}

$events = json_decode($json, true);
print_r(find_events($events, '2022-09-29 07:00'));

?>```

13
  • 2
    ...ok. and what specific problem are you having when you run your code? What's your actual question about this? Commented Sep 29, 2022 at 11:11
  • this gives me always 0 answers. even dates are equal... I Commented Sep 29, 2022 at 11:13
  • I want that if the dates match show data ...if not give me 0 answer Commented Sep 29, 2022 at 11:14
  • 1
    this gives me always 0 answers...no, it doesn't. Demo: 3v4l.org/3gsK3 Commented Sep 29, 2022 at 11:15
  • What PHP default time zone do you have set on your system? And btw., you realize that this would overwrite $r each time, in case there should ever be multiple matching events, yes? Commented Sep 29, 2022 at 11:16

1 Answer 1

2

Although the wording is not very clear, I am assuming you want to check if the date provided falls within the "from" and "to" dates. I also assume you want it to analyse all the dates in the array.

Right now it will just return the result from the last item in the array - since you are overwriting the result with every loop. And also, your comparison logic is wrong:

if ($date >= $from || $date <= $to)

says "if the date is after the 'from' date OR before the 'to' date". But to check if it's between those dates, clearly both conditions will need to be true.

To fix this, change the || to && in that line above, and also you can concatenate the results into a string, instead of overwriting them, so that you see every result.

function find_events($events, $date) {
    $date = new DateTime($date);
    $r = "";
    
    foreach ($events as $event) {
      $from = (new DateTime($event['start_datetime']));
      $to = (new DateTime($event['end_datetime']));
    
      if ($date >= $from && $date <= $to) {
           $r.= "{$event['start_datetime']} to {$event['end_datetime']}".'<br>';
      }
      else{
          $r.= "0<br>";
      }
    }
     
    return $r;
}

Demo: https://3v4l.org/30eIo

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.