How to get the difference between two days in php
I have a function passing some parameter like
everyWeekOn("Mon",11,19,00)
I want to calculate the difference between the current day (eg "Fri") and the parameter day passed, i.e. Mon
...
The output should be:
The difference between Mon and Fri is 3
I tried this like
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But I know that I don't know what to do next to get the difference between two days.
Note that this question is different from How do I calculate the difference between two dates using PHP? because I only have a day and not a full date.
+3
user4380380
source
to share
3 answers
Just implement the class DateTime
in conjunction with the method ->diff
:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
+3
source to share