0

I have two dates assigned in variable $date1 and $date2.. Here is the code..

if (isset($_POST['check_in']))
{
 $date1=date('Y-m-d', strtotime($_POST['check_in']));
}
if (isset($_POST['check_in']))
{
 $date2=date('Y-m-d', strtotime($_POST['check_out']));
}

For example if date1="2015-05-21" and date2="2015-05-23".I want the difference of date as 2

mas4
  • 989
  • 1
  • 8
  • 20

3 Answers3

1

Use DateTime class. Try with -

$date1=new DateTime("2015-05-21");
$date2=new DateTime("2015-05-23");

$interval = $date1->diff($date2);
echo $interval->format('%R%a days');

Output

+2 days

DateTime()

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

Here you go:

https://php.net/manual/en/datetime.diff.php

Code with various examples.

Here's one I like:

<?php
$datetime1 = date_create('2015-05-21');
$datetime2 = date_create('2015-05-23');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

I hope this helps :)

mas4
  • 989
  • 1
  • 8
  • 20
0

Since strtotime returns unixtime, the difference in seconds can be calculated by simply subtracting the one strtotime from the other:

$seconds = strtotime($_POST['check_out']) - strtotime($_POST['check_in']);

Then to find the days:

$days = $seconds / 60 / 60 / 24;
Jerbot
  • 1,168
  • 7
  • 18