1

I have a for each loop that loops through a set of lines each line has an installation date, but I only want to get the earliest installation date out of all the lines loop through, this could be any random line in the loop. How do I go about this? Should I just create an array of dates and then sort it or should I just check every time it loops? A code example would be best. The format of the date is simply: 2012-09-04

    foreach($lines as $line){


                $install_date = $line->installation_date_c;
                $water_cost = $line->water_cost_c;
                $energy_cost = $line->energy_cost_c;
                $oweeks = 52;
                $oyears = $line->operating_years_c;
                $default_curr = $line->currency_id;

         }
hakre
  • 193,403
  • 52
  • 435
  • 836
user794846
  • 1,881
  • 5
  • 29
  • 72
  • You can sort your array by date. Check [here](http://stackoverflow.com/questions/6401714/php-order-array-by-date) – Rikesh Mar 19 '13 at 11:18

2 Answers2

5

There are a lot of ways to accomplish this, maybe something roughly like this ?

$lowestDate = strtotime($lines[0]);
foreach($lines as $line){
    if(strtotime($line) < $lowestDate){
        $lowestDate = strtotime($line);
    }
}
echo "lowest date = " . date( 'y-m-d', $lowestDate);
randomizer
  • 1,619
  • 3
  • 15
  • 31
0

Try this :

$lines  = your array;
$sort = array();
foreach($lines as $key=>$line){
    $install_date[$key] = $line->installation_date_c;
 }

array_multisort($install_date, SORT_DESC, $lines);

echo "<pre>";
print_r(current($lines));
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73