I am trying to track the time of resolution of complaints in my application, so i have tried to get the difference between the time a problem was reported and date it was resolved as shown in code below. I have noticed that calculating the difference between the time expressions uses only the date parts of the values.
so 5-01-2014 minus 27-12-2013 returns -2 days instead of 9 days.
I have also noticed that the MySQL DATEDIFF() Function uses only the date parts of the values in the calculation too.
How may I get the correct duration returned as days?
Thanks.
<?php
require_once('./includes/connection.inc.php');
$conn = dbConnect('read');
$sql = 'SELECT
date_format(reported, "%d-%m-%Y") AS date_reported,
date_format(Date_Resolved, "%d-%m-%Y") AS date_resolved
FROM complaints ORDER BY created DESC';
$result = $conn->query($sql) or die(mysqli_error());
$numRows = $result->num_rows;
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Customer Complaints</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Resolution Period</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()) {
$date1=$row["date_reported"];
$date2=$row["date_resolved"]; ?>
<tr>
<td> <?php echo $date2-$date1; ?> days</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>