-1

How can take date difference using php?

I tried some code but showing error: date_format() expects parameter 1 to be DateTimeInterface, object given

This is the code I have tried

$checkin_from = 1502143200000
$reserve_to = 1502229600000
$days       = round(abs(strtotime(date_format($checkin_from , 'd.m.Y')) - strtotime(date_format($reserve_to, 'd.m.Y'))) / 86400);
Sarath TS
  • 2,432
  • 6
  • 32
  • 79
  • 1
    Possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Bartosz Zasada May 22 '17 at 06:51
  • Sticking random functions together is not really a good approach to programming. And formatting a date difference as "d.m.Y" is somewhat weird. – deceze May 22 '17 at 07:19

4 Answers4

0

You are using milliseconds instead of seconds first you have to divide them by 1000 to convert it into valid timestamp of seconds.

Try this code snippet here

<?php
ini_set('display_errors', 1);

$checkin_from = 1502143200000/1000;
$reserve_to = 1502229600000/1000;

$dateObj1= new DateTime();
$dateObj1->setTimestamp($checkin_from);

$dateObj2= new DateTime();
$dateObj2->setTimestamp($reserve_to);

$result=$dateObj1->diff($dateObj2);
print_r($result->d);   
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • `DateTime` can handle UNIX timestamps directly, going through `date` first is unnecessarily complicated. – deceze May 22 '17 at 07:37
  • @Sahil Gulati I tried to but getting and error. "Object of class MongoDB\BSON\UTCDateTime could not be converted to int" – Sarath TS May 24 '17 at 12:28
  • @samsam Can you tell me on which line you are getting this error because in my above post i haven't used anything related to mongo – Sahil Gulati May 24 '17 at 14:24
  • @Sahil Gulati, Dates are getting from mongodb and that date is storing in to variable `$checkin_from` and `$reserve_to`. – Sarath TS May 26 '17 at 10:59
  • @samsam Make sure you are getting this `1502143200000` in `$checkin_from`? – Sahil Gulati May 26 '17 at 11:01
-1

Use below code:

<?php
$checkin_from = '1502143200000';
$reserve_to = '1502229600000';
$datediff = $reserve_to - $checkin_from;

echo date('Y-m-d',$checkin_from)."\n";
echo date('Y-m-d',$reserve_to)."\n";
echo floor($datediff / (60 * 60 * 24));

Output

1935-04-17
1938-01-11
Total Days: 1000

Demo : Click Here

RJParikh
  • 4,096
  • 1
  • 19
  • 36
  • There are 1000 days between these two timestamps…?! – deceze May 22 '17 at 07:39
  • Please check output I have converted `strtotime()` to date its like difference of more then 365 days so answer 1000 days is Correct. @deceze – RJParikh May 22 '17 at 11:10
  • Well, no, those timestamps are in milliseconds and not in the 20th century, you're merely witnessing a number overflow. They're just a day apart. – deceze May 22 '17 at 11:48
-1

You can try below examples.

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Or you can try below.

$date1 = "2007-03-24";
$date2 = "2009-06-26";
$datediff = $date2 - $date1;

echo floor($datediff / (60 * 60 * 24));
Jalpa
  • 697
  • 3
  • 13
-1
<?php
$checkin_from = 1502143200000;
$reserve_to = 1502229600000;
$diff = $checkin_from - $reserve_to;
$days = abs($diff/86400*1000);
print $days;

86400 is an amount of seconds in one day

hvertous
  • 1,133
  • 11
  • 25