1

I have an array:

array(2) {
  ["Status"]=>
  string(3) "001"
  ["Data"]=>
  array(1) {
    ["item"]=>
    array(87) {
      [0]=>
      array(36) {
        ["StartDate"]=>
        string(10) "2017-01-13"
        ["StartTime"]=>
        string(8) "07:02:18"
      }
    }
  }
}

I'd like to basically selected data from it ONLY if the StartTime is in the last 30 seconds - according to server time.

What is the best way to do this?

6
  • 2
    server time changed continuously. so sometime happen that some index will missed( means some data). Commented Jan 15, 2017 at 9:00
  • @Anant Absolutely. It's just this array will also change, so new entries will be added every few seconds and my script needs to find those added in the last 30 seconds based on the time on the server. Commented Jan 15, 2017 at 9:04
  • 2
    grab the time and date strings and use strtotime() then compare strtotime() against time() Commented Jan 15, 2017 at 9:04
  • 1
    @Chris Thank You, Chris. Will look into this. Commented Jan 15, 2017 at 9:04
  • 1
    @michaelmcgurk then go whith what chris suggested Commented Jan 15, 2017 at 9:05

2 Answers 2

3

You can use array_filter() function:

$out = array_filter(
    $values, // your original array
    function($element)
    {
        $current_time = time();

        // change the keys to match your input 
        $element_time = strtotime($element['start_date'] . 
                                  ' ' . 
                                  $element['start_time']);

        // fix this condition according to your needs
        return $current_time - $element_time < 30;
    }
);

// now $out contains filtered values and $values is unchanged
Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks for this, Yury. StartTime is in the format 07:02:18 so will this filter work for me? I have tried an example (changing one value in my array to be in the last 30 seconds, but I got a NULL output :(
Your StartTime is inside a nested array, make sure to use a full path to it. Also, you need to use StartDate. Use var_dump inside the function to see what are the values that it gets and compares
Aaaah, so $element['start_date'] should be called something else?
Probably, $element['Data']['item']['StartDate']? It depends on your exact input.
0
function getFilteredCallsByDate($calls, $since) {
    return new CallbackFilterIterator(new ArrayIterator($calls['Data']['item']), function ($call) use ($since) {
        return strtotime(sprintf('%s %s', $call['StartDate'], $call['StartTime'])) >= strtotime($since);
    });
}

$calls = [
    'Data' => [
        'item' => [
            ['StartDate' => '2017-01-15', 'StartTime' => '07:38:18'],
            ['StartDate' => '2017-01-15', 'StartTime' => '08:38:18'],
            ['StartDate' => '2017-01-15', 'StartTime' => '11:25:59'],
        ]
    ]
];

var_dump($calls);

echo "The time is " . date("h:i:sa");

foreach (getFilteredCallsByDate($calls, '-30 seconds') as $call) {
    var_dump($call);
}

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.