0

I need to calculate difference between 2 dates here is my dates

$start = strtotime('17/05/2016');
$end = strtotime('12/05/2016');

I have tried

  echo $days_between = ceil(abs($end - $start) / 86400);

But it shows output as 17140

Hep to find the number of days between 2 given dates

vellai durai
  • 1,019
  • 3
  • 16
  • 38

3 Answers3

2

You just need to specify what is the date format, so:

$date1 = DateTime::createFromFormat('d/m/Y',"12/05/2016");
$date2 = DateTime::createFromFormat('d/m/Y',"17/05/2016");
echo $diff = $date2->diff($date1)->format("%a"); //output: 5
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
0

You need to change the format: with d/m/Y at m/d/Y

You can also use a format like this:

$s = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$d = DateTime::createFromFormat('d/m/Y', '05/12/2016');

And get the difference of days:

$start = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$end = DateTime::createFromFormat('d/m/Y', '12/05/2016');
$interval = $end->diff($start);
$days = $interval->format('%a');
Maxim Tkach
  • 1,607
  • 12
  • 23
0

You are using a wrong format for dates, the original is: "Y-m-d" not "d/m/Y", you can use something like this if you could change the date format:

 $now = strtotime("2016-05-17"); // or your date as well
 $your_date = strtotime("2016-05-12");
 $datediff = $now - $your_date;
 echo floor($datediff/(60*60*24));
perodriguezl
  • 430
  • 3
  • 13