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

Time:08-27

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. 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. 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[6]:

$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[6]:

string(19) "2022-04-07 13:34:10"

How can I add these values to the array?

CodePudding user response:

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);

CodePudding user response:

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