1
我被困在使用PHP的DateTime :: diff函数获取数周和数月。PHP - 如何得到几天,几周和几个月没有周末?
这里是我的代码:
$start = new DateTime('2017-06-05');
$end = new DateTime('2017-06-07');
$end->modify('+1 day');
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
// best stored as array, so you can add more than one
$holidays = array('2017-09-07');
foreach($period as $dt) {
$curr = $dt->format('D');
// substract if Saturday or Sunday
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}
// (optional) for the updated question
elseif (in_array($dt->format('Y-m-d'), $holidays)) {
$days--;
}
}
if($days <= 7) {
echo $days." day".($days > 1 ? 's' : '');
}
// I'm stucked here
我能得到的工作天数,但我不能让几个月和几个星期。
有人帮我解决这个问题吗?
这可能帮助:https://stackoverflow.com/a/7189826/7956549 – stevenkellow