-1

Here's my code.

$checkinDate = $_POST['checkinDate'];
$checkoutDate = $_POST['checkoutDate'];
$no_nights = date_diff($checkinDate, $checkoutDate);

My code error:

Warning: date_diff() expects parameter 1 to be DateTimeInterface, string given in C:\xampp\htdocs\lgh-hms\admin\conformation.php on line 19
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
dadadaxtr
  • 17
  • 4

1 Answers1

1

date_diff needs date, not string...

Procedural style:

$checkinDate = date_create($_POST['checkinDate']);
$checkoutDate = date_create($_POST['checkoutDate']);
$interval = date_diff($checkinDate,$checkoutDate);
$no_nights = $interval->format('%a');

With Objects:

$checkinDate = new DateTime($_POST['checkinDate']);
$checkoutDate = new DateTime($_POST['checkoutDate']);
$interval = $checkinDate->diff($checkoutDate);
$no_nights = $interval->format('%a');

I hope it helps

A. Iglesias
  • 2,625
  • 2
  • 8
  • 23