0

I have 3 array in my database that is coming from a form a form

array result form database

  • staff names דרור שליט,לנה שליט
  • start date 2019-02-01,2019-02-14
  • end date 2019-02-13,2019-02-27

I would like to check if the current date is in the range of the two arrays and if there is a match display the name of he staff member from the staff array for example if I take only the first value of each array the result will be

  • staff name לנה שליט
  • start date 2019-02-01
  • end date 2019-02-13

This is what I tried but that doesn't give me the proper result :

$neword = "SELECT `order_id` , `item`,`staff_name`, `staff_start_d`,`staff_end_d`,`item_sn`,`status`, DATE_FORMAT(`stamp` , '%d-%m-%Y'  ) as `stamp` FROM rent ";
$userord = mysqli_query($link, $neword);
foreach ($userord as $myitem);

  <?php foreach ($userord as $myitem) : ?>
<?php
      $s_dates = array($myitem['staff_start_d']);
      $e_dates = array($myitem['staff_end_d']);
      $names = array($myitem['staff_name']);
      $start = date("Y-m-d");


      foreach($s_dates  AS $date_s) {
          $timestamp = strtotime($date_s);
          if($timestamp == $start ) {
              echo "1";
          }else{
              echo "no1";
          }
      }

      foreach($e_dates  AS $date_e) {
          $timestamp = strtotime($date_e);
          if($timestamp == $start ) {
              echo "2";
          }else{
              echo 'no2';
          }
      }
      ?>

8
  • If you want to check if a date is in a given range, then why are you comparing for equality? That would get you matches only of the date happened to be the exact start or end date of the interval, but not if it was anywhere in between. And to check if a date was in any of those intervals, you would need to check it against start and end date of an interval - right now, you are checking it against all the start dates first, and then against all the end dates after - does not make much sense either. Commented Feb 25, 2019 at 11:35
  • You should really start with a better data structure. This should be one array (that you can then loop over), that contains intervals (start and end date) as its elements. Commented Feb 25, 2019 at 11:35
  • so you recommend to put all the data in the same column? like staff_name,start_date,end_date? Commented Feb 25, 2019 at 11:37
  • I am talking about your arrays here, not your database structure. (Those do not necessarily need to match 1:1.) Commented Feb 25, 2019 at 11:39
  • can you pleas give me an example for better data structure? Commented Feb 25, 2019 at 11:41

1 Answer 1

1

You need to convert date in timestamp and then you can check as below code:

$s_dates = explode(",", $myitem['staff_start_d']);
$e_dates = explode(",",$myitem['staff_end_d']);
$names = explode(",", $myitem['staff_name']);
$start = date("Y-m-d");


foreach($s_dates as $key => $date_s) {
    if (strtotime($start) >= strtotime($date_s) && strtotime($start) <= strtotime($e_dates[$key])) {
        // show your employee details here as
        echo $date_s.'<br/>';
        echo $e_dates[$key].'<br/>';
        echo $names[$key].'<br/>';
    }
}

Also, I am assuming that you are making value array which is useless. Hope it helps you.

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

7 Comments

thank yo for this comment , just for test if i change the strtotime($myitem['staff_start_d']) to the current date i get a result but the full array and not only the result that mach the date inside the array.. any advice?
“You need to convert date in timestamp” - no, you don’t need to; the input values are in a sortable format already, so the necessary checks can be done doing string comparison.
can you give me a code example based on Rohit Mittal answer?
@drorshalit I dont understand your comment. can you show your $userord vardump so I can understand better.
array(8) { ["order_id"]=> string(1) "1" ["item"]=> string(25) "חמישה בלונים 5" ["staff_name"]=> string(33) "דרור שליט,לנה שליט" ["staff_start_d"]=> string(21) "2019-02-01,2019-02-14" ["staff_end_d"]=> string(21) "2019-02-13,2019-02-27" ["item_sn"]=> string(11) "balonim001 " ["status"]=> string(1) "1" ["stamp"]=> string(10) "25-02-2019" }
|

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.