-1

How to Calculate and display the difference between two dates using PHP? i had try some of methods but i couldn't execute perfectly. anyone can help me

  S/N  | Event          | Duration 
 ----- | -------------  | ------------ 
  01   | Trip- Bangkok  | 2016-11-08 to 2016-11-20
  02   | Trip-New York  | 2016-12-01 to 2016-12-17

but i would like to display like this

  S/N  | Event          | Duration days
 ----- | -------------  | ------------ 
  01   | Trip- Bangkok  | 12 days
  02   | Trip-New York  | 17 days

My database

 <table border="1" width="920px" align="center" cellpadding="3" class="mytable" cellspacing="0" >
        <tr>
            <th>S/N</th>
            <th>Event</th> 
            <th>Duration</th>
        </tr>
  
       <?php
   $key="";
 if(isset($_POST['searchtxt']))
  $key=$_POST['searchtxt'];
 
 if($key !="")
  $sql_sel=mysql_query("SELECT * FROM trip_tbl NATURAL JOIN info_tbl WHERE info_name  like '%$key%' or event like '%$key%'");
 else
        $sql_sel=mysql_query("SELECT * FROM info_tbl  NATURAL JOIN trip_tbl");
  
    $i=0;
    while($row=mysql_fetch_array($sql_sel)){
    $i++;
    $color=($i%2==0)?"lightblue":"white";
    ?>
      <tr bgcolor="<?php echo $color?>">
       <!-- <tr><br><?php echo  $i;?></br></tr> -->
         <td><p align ="center"><?php echo $i;?></p></td>
            <td><font size="4px"><?php echo $row['event'];?></font></td>   
   <td><font size="4px"><?php echo $row['departure']." to ".$row['arrived'];?></font></td>
   
            </tr>
    <?php 
    }
    ?>
 <tr></tr>

:- I've used DATEDIFF but it's doesn't work out

Arul Satt
  • 1
  • 3

1 Answers1

1

If you have PHP version > 5.3 then you can calculate with this.

$date1 = new DateTime("2016-11-08");
$date2 = new DateTime("2016-11-20");

$diff = $date2->diff($date1)->format("%d");
echo $diff;

$diff will print your exact days.

Tejas Mehta
  • 791
  • 1
  • 6
  • 15