I'm trying to calculate the number of days since a specific date, but an error surfaces at the 87th day, and lasts till the 303th day, before it returns to normal. The error is always bellow the number with a decimal number of .958333333333.
function findDays($day, $month) {return ( (mktime(0, 0, 1, $month, $day)-strtotime("2022-01-01 00:00:01"))/86400) 1;}
$months=[31,28,31,30,31,30,31,31,30,31,30,31];
for($i=0;$i<12;$i ){
echo "<br>month-".($i 1).": ";
for($j=0;$j<$months[$i];$j ){
$days= findDays($j 1, $i 1);
echo $days." | ";
}
}
CodePudding user response:
If you simply want to know for a particular date how many days have passed since the start of the year, you can use a date format:
function findDays($day, $month) {
return DateTime::createFromFormat('m-d', $month . '-' . $day)
->format('z');
}
For today, April 1st 2022, that returns 90 (if you don't provide a year in the DateTime format, the current year is used).
If you want to get the number of days since a specific date, you can use a DateInterval:
function findDays($day, $month) {
$baseDate = new DateTime("2020-01-01");
return DateTime::createFromFormat('m-d', $month . '-' . $day)
->diff($baseDate)
->days;
}
For today, that returns that 821 days have passed since January 1st 2020.