SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast defIEd's Avatar
    Join Date
    Feb 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably $found=$j instead of $found=$i

  3. #3
    SitePoint Enthusiast defIEd's Avatar
    Join Date
    Feb 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah sorry that was a typo when I typed in the exampe.

    still doesn't work

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have a couple questions.

    given
    PHP Code:
    $hours = array(120);
    $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(911);
    $dateH 10
    what should the result be? or does it matter?

  6. #6
    SitePoint Enthusiast defIEd's Avatar
    Join Date
    Feb 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies!

    Quote Originally Posted by clamcrusher
    i have a couple questions.

    given
    PHP Code:
    $hours = array(120);
    $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...

    Quote Originally Posted by clamcrusher
    given
    PHP Code:
    $hours = array(911);
    $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. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $hours = array(119220);
    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. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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(280300350360380390450);
    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


Bookmarks

Posting Permissions

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