在foreach循环中以小时为单位获取两个日期之间的差异

What i want is to get difference between start and ends dates for user. there are 3 dates for a user which has start date and end dates. and all dates coming from database when i try i am unable to get desired result which is to get differences between dates such as 2 days, 3 days, 4 days from list of dates, and no error is showing.

My Code

<?php 
    $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)
        {  
            $diff = date_diff($result->ToDate, $result->FromDate);
            echo $diff->format("%h Hours");
            htmlentities($result->FromDate));
        }
    }
?>

Database:

enter image description here

Your date format in database is wrong, you have to fix that first by replacing the / with - eg.str_replace('/', '-', $result->ToDate))

then you have to convert the date to correct format like Y-m-d, after that you can check the difference, here is the solution for you

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

if you want the difference in hours you can try the below code

$hourdiff = abs(round(($datediff)/3600, 1));

I hope this will help you

Is there anything outputted by your echo statement?

As fat as PHP is concerned the value returned by mysql/pdo is a string.

You have at least two options.

From the first example in the php docs for date_diff: https://www.php.net/manual/en/datetime.diff.php

<?php
    $datetime1 = new DateTime('2009-10-11');
    $datetime2 = new DateTime('2009-10-13');
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%a days');
?>

Notice that they convert the date string to a date object first.

Then you can use date_diff or $someDateObj->diff($dateObjTwo)

An alternative that works just as well is to select the date fields as a unix timestamp, then subtract both timestamps and convert the remaining absolute value to time in the format you wish. I may follow up with an example of that later.

  • edit - answering on my tiny phone so I missed the date format issue

I ended up with this code this definitely help others.

$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>';
}

?>