我在传递一些参数的功能上喜欢
everyWeekOn("Mon",11,19,00)
我想计算当天的差额(例如“星期五”)
并通过参数日,即星期一
输出应为:
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'));
但是知道我不知道下一步该怎么做才能获得两天的差额.
解决方法:
只需结合-> 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');
标签:datediff,datetime,php,date
来源: https://codeday.me/bug/20191028/1952541.html