我有一个从数据库查询的日期时间数组。我想知道如何在php的循环中获取每个日期的时间间隔
实施例
Array('2014-11-03 13:00:00','2014-11-03 14:00:00','2014-11-03 16:30:00')
应输出
1:00
2:30
答案 0 :(得分:1)
如果我理解您的问题更正,则此代码段应该可以按您的要求运行:
$my_dates = array('2014-11-03 13:00:00','2014-11-03 14:00:00','2014-11-03 16:30:00');
$last_date = null;
foreach ($my_dates as $current_date) {
$dt = new DateTime($current_date);
if ($last_date) {
$diff = $dt->diff($last_date);
echo $diff->format('%H:%I')."\n";
}
$last_date = $dt;
}
答案 1 :(得分:0)
OK这是我对for循环的看法:
$ar = array('2014-11-03 13:00:00','2014-11-03 14:00:00','2014-11-03 16:30:00');
for($i=0;$i<count($ar)-1;$i++){
$d1 = new DateTime($ar[$i]);
$d2 = new DateTime($ar[$i+1]);
$time = $d1->diff($d2);
echo $time->h.':'.$time->i.'<br />';
}
干杯!