我有一个传递一些参数之类的函数
everyWeekOn("Mon",11,19,00)
我想计算当天(例如“周五”)之间的差异
并传递参数日,即 Mon
。
输出应该是:
The difference between Mon and Fri is 3
我试过这样的
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
但是我知道我不知道下一步该怎么做才能得到这两天之间的差异。
注意这个问题不同于How to calculate the difference between two dates using PHP?因为我只有一天而不是完整的约会。
最佳答案
只需结合 ->diff
方法实现 DateTime
类:
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');
关于php - 如何在php中获取两天之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691750/