简体   繁体   English

mysql和PHP中两个日期之间的差异

[英]Difference between two dates in mysql & PHP

I want to know the difference between two dates(joinon in table's field) of different id's in same table in PHP. 我想知道PHP中同一个表中不同id的两个日期(表的字段中的joinon)之间的区别。 Attachement of my table structure as attached below. 我的桌子结构的附件如下所示。

在此输入图像描述

Displayed dates are shown at the mentioned fields shown at the below screen shots. 显示的日期显示在下面屏幕截图所示的上述字段中。

[![enter image description here][2]][2] [![在此处输入图片说明] [2]] [2]

my php code are mentioned below 我的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>

I am getting the result like this format enter image description here 我得到的结果就像这种格式在这里输入图像描述

The logic of comparing two dates is as follows: 比较两个日期的逻辑如下:

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

In your case, it's probably easier if you change the structure a little so that all the array rows are stored in a single array before you start comparing dates. 在您的情况下,如果您稍微更改结构以便在开始比较日期之前将所有数组行存储在单个数组中,则可能更容易。

Replace your while() loop with the following: 用以下内容替换while()循环:

while ($getannexure[] = mysql_fetch_array($proj, MYSQL_ASSOC)){}

The run the following foreach() : 运行以下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;
    }
}

Now loop thru the $getannexure array like you have and you'll have an added diff value that you can use. 现在循环通过$getannexure数组,你可以使用添加的diff值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM