2013-03-19 82 views
0

對於每個循環來說,每一行都有一個安裝日期,但我只想從所有循環中得到最早的安裝日期,可以是循環中的任意一行。我如何去做這件事?我應該只是創建一個日期數組,然後對其進行排序,或者我應該每次檢查循環時都檢查一下嗎?代碼示例將是最好的。日期的格式很簡單:2012年9月4日在foreach循環中查找最早的日期

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; 

     } 
+0

您可以按日期排序數組。檢查[這裏](http://stackoverflow.com/questions/6401714/php-order-array-by-date) – Rikesh 2013-03-19 11:18:30

回答

2

有很多的方法可以做到這一點,也許事情大致是這樣的?

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

試試這個:

$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));