Henry Gunawan
Henry Gunawan

Reputation: 942

Count number of days between two dates should be two days with 1 day interval, but instead count as one day

I counted number of days between two dates, which is 24 July and 25 July. It should be two days, but I got one day. Here's the code:

$begin = new \DateTime("2020-07-24");
$end = new \DateTime("2020-07-25");

$interval = \DateInterval::createFromDateString('1 day');
$period = new \DatePeriod($begin, $interval, $end);

echo count($period); // => this return 1

What I want is two days instead of one. Would it be correct if I just need to add +1 to count($period)? Is there any other way to solve this problem?

Upvotes: 0

Views: 216

Answers (1)

Karthik Radhakrishnan
Karthik Radhakrishnan

Reputation: 944

Since you have not mentioned the time the time would be defaulted to 00:00

With the calculation you provided the difference between these two dates is indeed 1 day. However if you want the count to be 2 days. then add 1 day to your result, it will always be correct.

Upvotes: 1

Related Questions