I tried to create a php script that take a date from a daterangepicker, creates two variables that are then converted in strtotime.
I then divide the difference by 86400 to return the number of days, but instead of returning one (when I pick one day), it returns 31. Do you have any idea of where I made a mistake?
Example when $_POST['periode']=03/06/2018 00:00 - 04/06/2018 00:00
$datete=$_POST['periode'];
list($debut, $fin) = explode(" - ", "$datete", 2);
$debutTS=strtotime($debut);
$finTS=strtotime($fin);
$diff=($finTS-$debutTS)/86400
$debutTS
returns 1520294400 and $finTS
returns 1522972800. $debutTS
should return 1527976800 $finTs
should return 1528063200
Any idea?
You can use PHP's DateTime-class for this. It can also calculate the difference for you so there's no reason for doing that manually.
$dates = explode(' - ' , $_POST['periode']);
// Create the first date from your date/time format
$from = DateTime::createFromFormat('d/m/Y H:i', $dates[0]);
// Create the second date from your date/time format
$to = DateTime::createFromFormat('d/m/Y H:i', $dates[1]);
// Get the difference
$interval = $from->diff($to);
// Get the difference in days (it's the %a token)
// No need to calculate anything manually
$diff = $interval->format('%a');
echo $diff;
Demo: https://3v4l.org/HVukA