假设我有一个日期,想要在下周一或周三到达,这距离日期更近。
$date = 20211002;
$date_object = DateTime::createFromFormat('Ymd', $date);
$next = $date_object->modify('next wednesday')->format('d/m/Y');
是否可以设置两个或两个以上的工作日?
不,但你可以解决这个问题:
// Closest "next" based on weekday ($date_object->format('w'))
// which is 0 (for Sunday) through 6 (for Saturday)
// 0 => monday (from sunday)
// 1 => wednesday (from monday)
// 2 => wednesday (from tuesday)
// 3 => monday (from wednesday)
// 4,5,6 => monday
因此,只有format('w')为1或2时,第二天才是下星期三。
因此,
$go = in_array(
$date_object->format('w'),
[ 1, 2 ]
) ? 'next wednesday' : 'next monday';
$next = $date_object->modify($go)->format('d/m/Y');