简体   繁体   中英

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. 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]

my php code are mentioned below

<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 ($getannexure[] = mysql_fetch_array($proj, MYSQL_ASSOC)){}

The run the following 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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