use*_*846 1 php arrays datetime
我有一个for each循环,它通过一组线循环,每条线都有一个安装日期,但是我只想从所有循环中获得最早的安装日期,这可以是循环中的任何随机线。我该怎么办?我应该只创建一个日期数组然后对其进行排序还是应该在每次循环时检查一下?一个代码示例将是最好的。日期的格式很简单: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;
}
Run Code Online (Sandbox Code Playgroud)
有很多方法可以做到这一点,也许大概是这样的?
$lowestDate = strtotime($lines[0]);
foreach($lines as $line){
if(strtotime($line) < $lowestDate){
$lowestDate = strtotime($line);
}
}
echo "lowest date = " . date( 'y-m-d', $lowestDate);
Run Code Online (Sandbox Code Playgroud)