1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

High Tide, Low Tide

Discussion in 'PHP' started by sarahk, Sep 7, 2014.

  1. #1
    I've found a site that gives predicted tide levels - every 6 minutes! That's probably grand for people who need to know that stuff but I just want to know when high tide is, when low tide is.

    I'm sure there's a natty way to sort this out but I'm not getting my head around it.

    Any hints on how to process the feed to find the 2 highs and 2 lows for the day?
     
    sarahk, Sep 7, 2014 IP
  2. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #2
    Sounds like an array sort and key => value return problem I worked on a while back. If you can import recorded times to keys and water heights to values then this modded code from my archive should do the trick.

    
    <pre>
    <?php
    /*
    http://tides.willyweather.com.au/qld/mackay/mackay-harbour.html
    Sample, Mackay Harbour Tides - Tide Curve, Wed Sep 10th.
    */
    
    /*import data to associative array Time => Height*/
    $array = array("12:04AM"=>"5.8M",
                   "12:24AM"=>"5.5M",
                   "12:44AM"=>"5.2M",
                   "1:03AM"=>"4.8M",
                   "1:23AM"=>"4.4M",
                   "2:02AM"=>"3.5M",
                   "2:22AM"=>"3.1M",
                   "2:41AM"=>"2.6M",
                   "3:01AM"=>"2.1M",
                   "3:20AM"=>"1.7M",
                   "3:40AM"=>"1.3M",
                   "4:00AM"=>"0.9M",
                   "4:19AM"=>"0.6M",
                   "4:39AM"=>"0.3M",
                   "4:58AM"=>"0.2M",
                   "5:18AM"=>"0.0M");
             
    natsort($array);
    //print_r($array); echo "<br>"; debugging only.
    
    /* #LOW TIDE, return first and second key => value pair */
    $FirstVal = reset($array);
    $FirstKey = key($array);
    $SecondVal = next($array);
    $SecondKey = key($array);
    
    /* #HIGH TIDE, return second last and last key => value pair */
    $LastVal = end($array);
    $LastKey = key($array);
    $SecondLastVal = prev($array);
    $SecondLastKey = key($array);
    
    echo "#LOWEST TIDE:\t\t", $FirstKey, " ", $FirstVal, "<br>",
         "#2nd LOWEST TIDE:\t", $SecondKey, " ", $SecondVal, "<br><br>",
         "#HIGHEST TIDE:\t\t", $LastKey, " ", $LastVal, "<br>",
         "#2nd HIGHEST TIDE:\t", $SecondLastKey, " ", $SecondLastVal;
    
    /*
    returns,
    #LOWEST TIDE:          5:18AM 0.0M
    #2nd LOWEST TIDE:     4:58AM 0.2M
    
    #HIGHEST TIDE:        12:04AM 5.8M
    #2nd HIGHEST TIDE:   12:24AM 5.5M
    */
    ?>
    </pre>
    PHP:

    I'm not familiar with the tide data that your actually using but I got a sample
    from a random weather site for the code to test.



    .
     
    Last edited: Sep 9, 2014
    ROOFIS, Sep 9, 2014 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Thanks @ROOFIS, that's pretty much the approach I'd thought of too, but prev/next won't work, I'll need to actually work down the array to ensure the time on the next/prev is more than an hour different. On the live data sample I used the highs and lows were quite different and there were points mid cycle where the tide actually changed direction and then went back to normal. There must be some funky maths going to generate the predictions.
     
    sarahk, Sep 9, 2014 IP
  4. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #4
    No prob's, just to point out though the natsort($array); function in the script will sort the data from lowest to highest regardless of the data's hierarchy.

    For next/prev to be more than an hour different however you'll need to traverse though the keys with a boolean to calculate the differences between them to ensure you return results that are an hour apart from each other, if that's what your saying?



    .
     
    Last edited: Sep 9, 2014
    ROOFIS, Sep 9, 2014 IP