[英]Difference between two dates in mysql & PHP
我想知道PHP中同一个表中不同id的两个日期(表的字段中的joinon)之间的区别。 我的桌子结构的附件如下所示。
显示的日期显示在下面屏幕截图所示的上述字段中。
[![在此处输入图片说明] [2]] [2]
我的PHP代码如下所述
<table id="dataTableExample1" class="table table-bordered table-striped table-hover">
<thead>
<tr class="info">
<th class="text-center">Days</th>
<th>Date</th>
<th>Title</th>
<th>Time Interval</th>
<th>Attachment</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$proj_id = $_GET['proj_id'];
$proj = mysql_query("select * from project_historytl where proj_id='$proj_id' order by proj_histl_id desc") or die(mysql_error());
///echo "select * from project_historytl where proj_histl_id='$proj_id' order by proj_histl_id desc";exit();
$s = 0;
while ($getannexure = mysql_fetch_array($proj))
{
$s++;
?>
<tr>
<td class="text-center">Day <?php echo $s;?></td>
<td><?php echo $getannexure['joinon']; ?></td>
<td><?php echo $getannexure['proj_history_title']; ?></td>
<td> </td>
<td><a href="uploads/<?php echo $getannexure['attachment']; ?>" target="_blank">View</a> </td>
<td>
<a href="#" class="btn btn-add btn-sm"><span class="elusive icon-pencil" title="edit"></span><i class="fa fa-pencil"></i></a>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#customer2"><i class="fa fa-trash-o"></i> </button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
我得到的结果就像这种格式在这里输入图像描述
比较两个日期的逻辑如下:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
在您的情况下,如果您稍微更改结构以便在开始比较日期之前将所有数组行存储在单个数组中,则可能更容易。
用以下内容替换while()
循环:
while ($getannexure[] = mysql_fetch_array($proj, MYSQL_ASSOC)){}
运行以下foreach()
:
foreach($getannexure as $id => $row) {
if (!empty($getannexure[$id + 1])) {
$datetime1 = new DateTime($row[$id + 1]['joinon']);
$datetime2 = new DateTime($row[$id]['joinon']);
$interval = $datetime1->diff($datetime2);
$getannexure[$id]['diff'] = $interval->format('%R%a days');
} else {
$getannexure[$id]['diff'] = NULL;
}
}
现在循环通过$getannexure
数组,你可以使用添加的diff
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.