3 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
TA贡献1797条经验 获得超6个赞
在数据库中的日期格式是错误的,你必须修复,首先通过更换/带有-如。str_replace('/', '-', $result->ToDate))
那么您必须将日期转换为正确的格式,例如Y-m-d,之后您可以检查差异,这是适合您的解决方案
$to = date('Y-m-d', strtotime(str_replace('/', '-', $result->ToDate)));
$from = date('Y-m-d', strtotime(str_replace('/', '-', $result->FromDate)));
$datediff = strtotime($to) - strtotime($from);
echo abs(round($datediff / (60 * 60 * 24)));
如果您想要小时数的不同,可以尝试以下代码
$hourdiff = abs(round(($datediff)/3600, 1));
我希望这能帮到您
![?](http://img1.sycdn.imooc.com/545864000001644402200220-100-100.jpg)
TA贡献1831条经验 获得超4个赞
我最终得到了这段代码,这肯定可以帮助其他人。
$eid=$_SESSION['eid'];
$sql = "SELECT empid,ToDate,FromDate from tblleaves where empid=:eid";
$query = $dbh->prepare($sql);
$query->bindParam(':eid',$eid,PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 0)
{
foreach($results as $result)
{
$to = date('Y-m-d', strtotime(str_replace('/', '-', $result->ToDate)));
$from = date('Y-m-d', strtotime(str_replace('/', '-', $result->FromDate)));
$hours = round(abs(strtotime($from) - strtotime($to))/60/60).'<br>';
$sum+=$hours;
}
echo '<h5>'.$sum.' Hours '.'</h5>';
}
?>
- 3 回答
- 0 关注
- 150 浏览
添加回答
举报