2

I need to find the difference between 2 dates from an array. Here is the array:

$lists[] = date('Y-m-d', strtotime($user->created_at));

Example output of echo $lists[1]; is 2017-08-01

I have tried

$difftime = $lists[1]->diff($lists[0]);
echo $difftime->format('d');

and echo $lists[1]->diff($lists[0]);

Both giving me error:

Call to a member function diff() on string

I have also tried echo date_diff($lists[1], $lists[0]);

It's giving error

date_diff() expects parameter 1 to be DateTimeInterface, string given

I know there are many questions on same issue here, but I could not find a solution that works for me. So please help me.

Ahsan
  • 1,289
  • 3
  • 13
  • 37

2 Answers2

5

Before you can calculate the differences using DateTimeInterface::diff(), you need to create instances of DateTimeInterface first, for example:

$dateOne = \DateTimeImmutable::createFromFormat(
    'Y-m-d',
    $lists[0]
);

$dateTwo = \DateTimeImmutable::createFromFormat(
    'Y-m-d',
    $lists[1]
);

$difference = $dateTwo->diff($dateOne);

Note that $difference will be an instance of DateInterval - if you need the difference as a string, you need to use $difference->format() with the desired format.

For reference, see:

localheinz
  • 9,179
  • 2
  • 33
  • 44
  • 2
    I don't know who's been downvoting you or why, because yours is the more complete answer. – Sammitch Aug 25 '17 at 19:12
  • Thank you, @Sammitch, I appreciate it! – localheinz Aug 25 '17 at 19:15
  • Hi, I am not getting error, but the result is unexpected. $list[0] date is 2017-07-20 and $lists[1] is 2017-08-01. so the result should be 10 days, but I am getting 19. I added this in the end: $difftime = $difference->format('d'); echo $difftime; out put is 19. Am I missing something? – Ahsan Aug 25 '17 at 19:21
  • 2
    Use `$difference->format("%a");`. @localheinz's example is working correctly. It's 12 days btw. [Demo](https://3v4l.org/K2dt3) – ishegg Aug 25 '17 at 19:27
  • yes. working now with both '%d' and '%a'. Other answer by @Alive to Die is also working. I don't know which one should I accept... :-( – Ahsan Aug 25 '17 at 19:33
  • Question has already been closed as duplicate, no worries! – localheinz Aug 25 '17 at 19:34
  • Question may be duplicate, but problem is not. other solutions did not work for me :( – Ahsan Aug 25 '17 at 19:35
  • Thank you for the solution... – Ahsan Aug 25 '17 at 19:35
  • 2
    AFAIK you can still accept an answer after the question has been marked as duplicate. You should accept the answer that helped you the **most**. – ishegg Aug 25 '17 at 19:43
  • I am sorry @localheinz, although your answer works perfectly and gives clearer picture, I am selecting Alive to Die 's answer because it was first. – Ahsan Aug 25 '17 at 19:50
  • No problem, @Ahsan! – localheinz Aug 25 '17 at 19:54
1

Reference:-diff()

It takes DateTimeInterface as an parameter and it will called also on a DateTimeInterface only.

So do like below:-

<?php
$lists=array('2017-08-23','2017-08-26');

$datetime1 = new DateTime($lists[0]);
$datetime2 = new DateTime($lists[1]);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

Output:- https://eval.in/850404

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98