0
How to get days between two dates
guys i tired everything at least i posted it here if any can help me to get number of days between two dates with php only. A user enter two dates with html inputs and get number of days. i also tried php date_diff but got 0 value.. i dont know where i am wrong
9 ответов
+ 5
The problem with that code is that date_diff expects both parameters to implement the DateTime interface, and by default you are only sending two parameters of the string type. Another problem I had was the format of the dates, I showed them in D-M-Y format (although this may be due to my regional configuration)
Basically I just added the string conversion to date and I made sure to give them an indicated format:
<form action="" method="post">
<input type="date" name="d1">
<input type="date" name="d2">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$d1 = new DateTime(date('Y-m-d', strtotime($_POST['d1'])));
$d2 = new DateTime(date('Y-m-d', strtotime($_POST['d2'])));
$days = $d2->diff($d1);
echo "$days->d";
}
?>
+ 6
At this point I can think of two things:
1. Are you converting the dates? If I remember correctly, PHP will interpret the dates as a string.
2. What format are you using for the dates?
PS: You could also post your code here, that way it's easier for me to find a solution :)
+ 4
You're welcome!
+ 3
Are you sure you are using the diff() function well?
https://code.sololearn.com/woFUS8lpi7nY/?ref=app
+ 3
Ok
+ 1
mickel sanchez its working great thanks
0
@mickel i already use this but its not working when i choose date from 《input type="date"》
0
<?php
if(isset($_POST['submit']))
{
$d1 = $_POST ['d1'];
$d2 = $_POST ['d2'];
$days = date_diff($d2,$d1) ;
echo "$days";
}
?>
<form action="" method="post">
<input type="date" name="d1">
<input type="date" name="d2>">
<input type="submit" name="submit">
0
let me try it