Push in array result if datetime is greater than or less than 10 minutes of the createdDatetime

Viewed 34

I don't have much knowledge in php and I didn't find something to help me, you are my last hope.

I have a variable that receive a datetime and a variable that receive an empty array.

I do a foreach on an object that I call $file. This object has a property ($file[6]) which is the datatime and another with the name ($file[0]). I need to add the object to the array IF the datetime of $file[6] is greater than 10 minutes or less than 10 minutes of createDate and return a array with the results ( [ $file[0], $file[6] ] ). As you can see in the code below:

$createDate = $prop['created_date'];
$result = array();

foreach($obj['files'] as $file) {

  if (???) {
    // Here I need to compare one date with another and if the date is 
    // greater than 10 minutes OR less than 10 minutes I take this result 
    // and add it into the empty array, if not, I do nothing.
    array_push($result, $file)
  }
  
}

echo json_encode($result);

Var_dump results in $createDate and $file:

$createDate:

DataType: json Message: object(IP_DateTime)#77 (3) {
["date"]=>
string(26) "2022-04-25 03:38:15.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(17) "America/Sao_Paulo"
}

$file:

    array(9) {
[0]=>
string(26) "20220407_10_22_30_L100.SVL"
[1]=>
int(2)
[2]=>
string(8) "20220407"
[3]=>
int(4766)
[4]=>
int(307)
[5]=>
int(101366)
[6]=>
string(19) "2022-04-07 13:34:10"
[7]=>
int(0)
[8]=>
int(1)
}

How can I add these values to the array?

2 Answers

Compare the values based on their unix epoch. Here is an intentionally verbose example walking through the logic.

$createDate = $prop['created_date'];
$result = array();

foreach($obj['files'] as $file) {

  // get the file's date/time
  $fileDateTime = $file[6];

  // get the difference in seconds between createDate and the file's timestamp. Note that this is converting it into seconds since unix epoch using strtotime()
  $differenceInSeconds = strtotime($fileDateTime) - strtotime($createDate);

  // convert the result into an absolute value (always positive) given we want to know if it's more than 10 minutes after OR before
  $differenceInSeconds = abs($differenceInSeconds);

  // set our comparison; in this case, 10 minutes
  $threshold = 10 * 60; // 10 minutes

  // if the file has a timestamp that is greater than the threshold
  if ($differenceInSeconds > $threshold) {

     // add it to the array using php's shorthand syntax for adding to arrays
     $result[] = $file;

  }
  
}

echo json_encode($result);

I don't know how your time format is. demo:

$a = strtotime("2020-08-27 11:44:00"); //$createDate = $prop['created_date'];
$f = strtotime("2020-08-27 11:56:00"); //$file[6]
if($f > strtotime("+10 minutes",$a)){ //$file[6] > $createDate+ 10 minutes
    echo "yes";
}
// yes

ok, "2022-04-25 03:38:15.000000" still applies. so

$createDate = $prop['created_date'];
$result = array();

foreach($obj['files'] as $file) {

  if (strtotime($file[6]) > strtotime("+10 minutes",strtotime($createDate["date"]))) { //$file[6] > $createDate+ 10 minutes. $createDate["date"]? may be $createDate->date.
    array_push($result, $file)
  }
  
}

echo json_encode($result);
Related