Insert days between 2 date into database

I have 2 date input in html

<tr>
    <td>Value Date</td>
    <td><input type='date' name='valueDate' class='form-control'></td>                  
</tr>
<tr>
    <td>Maturity Date</td>
    <td><input type='date' name='maturityDate' class='form-control'></td>                   
</tr>

and this for action code

$valueDate = $_POST['valueDate'];
$maturityDate = $_POST['maturityDate'];

right now, i have a variable $days for days between 2 date, and thats initial like this :

$days = date_diff($valueDate,$maturityDate);

and this my query :

$query = mysqli_query($conn, "INSERT INTO placement(
                        valueDate, 
                        maturityDate,
                        days
                        ) VALUES(
            '$valueDate',
            '$maturityDate',
            '$days'
            )");

im using PHP 7.2.0 for using date_diff.

Before, i try to use $days->format("%a");

but i get error. Any idea?

Upvotes: 0

Views: 80

Answers (3)

Mohamed El Mrabet
Mohamed El Mrabet

Reputation: 633

Try this code:

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

Upvotes: 1

Isaac
Isaac

Reputation: 804

This should work

$date1 = new DateTime($_POST['valueDate']);
$date2 = new DateTime($_POST['maturityDate']);
$interval = $date1->diff($date2);

Upvotes: 1

Alex
Alex

Reputation: 17289

http://php.net/manual/en/class.dateinterval.php

//$d1=new DateTime("2018-07-08"); 
$d1=new DateTime($_POST['valueDate']);
//$d2=new DateTime("2018-10-08"); 
$d2=new DateTime($_POST['maturityDate']);
$interval = $d2->diff($d1)->days; 

https://ideone.com/hpkfxq

Upvotes: 0

Related Questions