# Thread: Find Closest hour from array

1. ## Find Closest hour from array

Hello,

I have an array - with 4 values.
Each value is an hour , like 23 or 5.

I need to find the closest hour, compared to date("H").
like \$hours = array { 1, 19, 22, 0 }; \$hour = date("H");

So if date("H") was 23, it would display 22.
If date("H") was 2, it would display 1
if date("H") was 0, it would display 0

I tried to substract each hour from the current hour, and take the one with the smallest substraction, but that did not seem to work.

PHP Code:
``` \$j = 0; \$tmp = 24; \$found = 0; while (\$hours[\$j]) { if ((\$hour - \$hours[\$j]) < \$tmp) { \$tmp =(\$hour - \$hours[\$j]); \$found = \$i; } \$j++; } echo "Found " . \$found;  ```
If someone could help me, it would be greatly appreciated.

Thanks,
Joe

2. Probably \$found=\$j instead of \$found=\$i

3. yeah sorry that was a typo when I typed in the exampe.

still doesn't work

4. I'm sure there's a better way to do it but I've been working on this a bit and have a lot of distractions:

PHP Code:
``` \$j = 0; \$tmp = 24; \$found = 0; \$hours = array(1,19,22,0); \$hour = date("H"); \$diff = array(); \$diffa = array();     while (\$hours[\$j]) {       \$found = \$hour - \$hours[\$j];       \$founda = \$hours[\$j];       if (\$found >= 0) {         \$diff[] = \$found;         \$diffa[] = \$founda;       }       \$j++;     } list(\$pop) = array_keys(\$diff,min(\$diff)); echo "Closest hour is: \$diffa[\$pop]";  ```
Just edit it to fit your script.

5. i have a couple questions.

given
PHP Code:
``` \$hours = array(1, 20); \$dateH = 23;  ```
should the result be 1?
its closer if its allowed to goto the next day
same thing, what about previous day?

given
PHP Code:
``` \$hours = array(9, 11); \$dateH = 10;  ```
what should the result be? or does it matter?

6. Thanks for the replies!

Originally Posted by clamcrusher
i have a couple questions.

given
PHP Code:
``` \$hours = array(1, 20); \$dateH = 23;  ```
should the result be 1?
its closer if its allowed to goto the next day
same thing, what about previous day?
The result should be 20, I want the result as close as possible to an hour that already passed. So as 1 is a new day, and the hour hasn't passed yet, it should find 20...

Originally Posted by clamcrusher
given
PHP Code:
``` \$hours = array(9, 11); \$dateH = 10;  ```
what should the result be? or does it matter?
The result should be 9, as the 9th hour passed, 11 has not passed yet.

Thanks for the help

Joe

7. PHP Code:
``` \$hours = array(1, 19, 22, 0); sort(\$hours); \$now = 2; foreach (\$hours as \$hour) {     if (\$hour > \$now) {         break;     }     \$result = \$hour; } echo \$result;  ```
\$result will be undefined if the \$hours array does not contain a value less than or equal to the \$now hour, because you said it cannot goto a previous day, or a future time.

8. I was looking for a similar script, where I can get the closet number from a given unknown array, if I am supplied with a variable. Thanks for the nice script clamcrusher. Added a couple of lines though ... (as I was needing the closest number, not the closest smaller number).

PHP Code:
``` \$hours = array(280, 300, 350, 360, 380, 390, 450); sort(\$hours);  \$now = 320;  foreach (\$hours as \$hour) {      if (\$hour > \$now) {          break;      }      \$result = \$hour;      \$result2 = next(\$hours);      (\$result2-\$now)<(\$now-\$result)?\$newresult = \$result2 : \$newresult = \$result;  }  echo  \$newresult;  ```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•