shank shank - 3 months ago 14
PHP Question

Difference between two dates in codeigniter

abscondingsince is a date field with value 2016-08-24 and dateofcontactviaphone is a date field with value 2016-08-26 . when I try to

echo timespan($row['absconding since'], $row['dateofcontactviaphone']);
its giving 2016-08-2446 Years, 8 Months, 3 Days, 5 Hours, 43 Minutes as the output whereas it should have been 2 days and so on. I have no clue where I am making a mistake. My view code is as below:



<table cellpadding="10px" border="1" style="background:none;width:75%;" RULES="ROWS" class="tab_data">
<thead>
<th>Employee Name</th>
<th>Absconding Since</th>
<th>Absconding Days</th>
<th>Phone Log</th>
<th>Comments on call made</th>
<th>Date of Email Sent</th>
<th>Comments/responses to email recorded</th>
<th>View Absconding Checklist</th>
</thead>
<tbody>
<?php
$serial_no = 1;
$i = 0;
foreach($rows as $row){
$i++;


?>
<tr>
<td style="width:10%">
<?php echo $row['name']; ?>
</td>
</td>
<td style="width:10%">
<?php echo $row['abscondingsince'];
echo timespan($row['abscondingsince'], $row['dateofcontactviaphone']);
?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaemail']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsemail']; ?>
</td>
<td style="width:10%">
<?php
if ($row['last_status'] == 'Accepted'){
echo anchor('exits/view_exit_checklist/'.$row['id'],"<i class='fa fa-eye edit_row' alt='View' title='Edit' rel='".$row['id']."' ></i>",array('rel'=>$row->id,'class'=>'edit_row'));
}
else
{
echo "NA";
}
?>
</td>
</tr>
<?php
} ?>
</tbody>
</table>

Answer

Use like this:-

echo timespan(strtotime($row['absconding since']), 
               strtotime($row['dateofcontactviaphone']));