I need to get date of next nearest weekday from today.( For ex : I will give three random weekdays like Sunday, Monday, Saturday. From this list I need to get next nearest weekday from today ( Thursday ) output is should be Saturday. Here after I want to get date of coming Saturday.
Asked
Active
Viewed 129 times
-2
-
Did you try anything? – takendarkk Dec 24 '15 at 04:33
-
Hint: `java.time.LocalDate.now( ZoneId.of( "America/Montreal" ) ).with( org.threeten.extra.Temporals.nextWorkingDay() )` – Basil Bourque Mar 14 '18 at 23:15
3 Answers
0
Can not understand your question clearly.. I suggest a solution based on my understanding... You should create a hash that map a weekday with a number, example:
{
Sun => 0,
Mon => 1,
...
Sat => 6
}
Then get the MIN absolute value of current day with the list of your random weekdays.

hienvd
- 346
- 1
- 5
- 17
0
Try this function:
echo nearestWeekend( '26-12-2015' );
function nearestWeekend( $date ){
$date = date('d-m-Y', strtotime($date . '+1 day'));
while( !preg_match('/Sunday|Saturday/i' , date('l', strtotime($date)) ) ){
$date = date('d-m-Y', strtotime($date . '+1 day'));
}
return date('d-m-Y', strtotime($date));
}

Vegeta
- 1,319
- 7
- 17