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

26th Apr 2018, 5:29 AM
Harry Singh
Harry Singh - avatar
9 Respuestas
+ 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"; } ?>
26th Apr 2018, 6:55 AM
Mickel
Mickel - avatar
+ 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 :)
26th Apr 2018, 6:16 AM
Mickel
Mickel - avatar
+ 4
You're welcome!
27th Apr 2018, 4:53 AM
Mickel
Mickel - avatar
+ 3
Are you sure you are using the diff() function well? https://code.sololearn.com/woFUS8lpi7nY/?ref=app
26th Apr 2018, 5:57 AM
Mickel
Mickel - avatar
+ 3
Ok
26th Apr 2018, 6:57 AM
Mickel
Mickel - avatar
+ 1
mickel sanchez its working great thanks
27th Apr 2018, 4:51 AM
Harry Singh
Harry Singh - avatar
0
@mickel i already use this but its not working when i choose date from 《input type="date"》
26th Apr 2018, 6:08 AM
Harry Singh
Harry Singh - avatar
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">
26th Apr 2018, 6:30 AM
Harry Singh
Harry Singh - avatar
0
let me try it
26th Apr 2018, 6:56 AM
Harry Singh
Harry Singh - avatar