Let's say I have a date and want to get the next monday OR wednesday, which was more closer from the date.
$date = 20211002;
$date_object = DateTime::createFromFormat('Ymd', $date);
$next = $date_object->modify('next wednesday')->format('d/m/Y');
Is possible to set two or more weekdays?
CodePudding user response:
No, but you can work around the issue:
// 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
So the next day is next wednesday only if format('w') is 1 or 2.
Therefore,
$go = in_array(
$date_object->format('w'),
[ 1, 2 ]
) ? 'next wednesday' : 'next monday';
$next = $date_object->modify($go)->format('d/m/Y');