3 回答

TA貢獻(xiàn)1797條經(jīng)驗 獲得超6個贊
在數(shù)據(jù)庫中的日期格式是錯誤的,你必須修復(fù),首先通過更換/帶有-如。str_replace('/', '-', $result->ToDate))
那么您必須將日期轉(zhuǎn)換為正確的格式,例如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)));
如果您想要小時數(shù)的不同,可以嘗試以下代碼
$hourdiff = abs(round(($datediff)/3600, 1));
我希望這能幫到您

TA貢獻(xiàn)1831條經(jīng)驗 獲得超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 關(guān)注
- 168 瀏覽
添加回答
舉報