-2
<?php
$date1 = "2002-1-1";
$date2 = "2021-12-31";
$deff = abs(strtotime($date2) - strtotime($date1));
$daycount = $deff / (60 * 60 * 24);
echo $daycount;
?>

Why the answer is 7304 instead of 7300 as I am trying to calculate the number of days for 20 years?

Please help.

John Conde
  • 217,595
  • 99
  • 455
  • 496

4 Answers4

2

Use DateTime for this:

$datetime1 = new DateTime("2002-1-1");
$datetime2 = new DateTime("2021-12-31");
$interval  = $datetime1->diff($datetime2);
$elapsed   = $interval->format('%a days');
echo $elapsed;

See it in action

Reference

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

You are forgetting leap years.

Nanne
  • 64,065
  • 16
  • 119
  • 163
0

Your code is correct. That is how many days there are between the two days. Don't forget about leap years.

mcryan
  • 1,566
  • 10
  • 20
0

Dude, the answer is:

From and including: Tuesday, 1 January 2002

To, but not including : Friday, 31 December 2021

It is 7304 days from the start date to the end date, but not including the end date.

You forgot to add the leap years. That is the four extra days!!!

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252