0

I have an time array and the frequency of reading is 5 seconds

I want to check if the frequency of reading is 5 second between every element of

array

My code

$freq_of_reading = 5;
$res_arr = array(
    0 => array("13:53:45"),
    1 => array("13:53:50"),
    2 => array("13:53:55"),
    3 => array("13:54:00"),
    4 => array("13:54:05"),
    5 => array("13:54:10"),
    6 => array("13:54:15"),
    7 => array("13:54:20"),
    8 => array("13:54:25"),
    9 => array("13:54:30"),
);


try {
    $u = 0;
    foreach ($res_arr as $key => $item) {
        // if(strtotime($item))
        //Logic here 
    }
} catch (Exception $e) {
    echo $exp = $e->getMessage();
}
3
  • What have you tried, what result are you currently getting, and what is the desired result? You haven't given us enough information to help you. Commented Feb 19, 2014 at 18:47
  • so... if ((arr[i] - arr[i-1]) > 5 seconds)) { Ooops! } Commented Feb 19, 2014 at 18:52
  • It seems that he want to check if the frequency of reading is correct all over the array. Commented Feb 19, 2014 at 18:52

3 Answers 3

1

Try this loop:

for($i = 0; $i < count($res_arr) - 1; $i++)
{
    if(strtotime($res_arr[$i + 1][0]) - strtotime($res_arr[$i][0]) != $freq_of_reading)
    {
        echo "Element $i differs from the next one more than $freq_of_reading s";
        exit;
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this code

$count = count($res_arr);
for($i=0;$i<$count$i++)
    {
        $number = intval(substr($res_arr[$i], -2));

        $number1 = substr(($res_arr[$i+1], -2));
        if(($number1-$number) == 5)
        {
          echo "true";
        }

    }

Comments

0

Not sure why that's in a try, but:

$freq_of_reading = 5;
$previous = 0;

foreach($res_arr as $item) {
    $time = strtotime($item[0]);
    if($time - $freq_of_reading == $previous) {
        echo 'yes';
    }
    $previous = $time;
}

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.