-2

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.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Gugan Abu
  • 546
  • 1
  • 4
  • 17

3 Answers3

0
date('l', strtotime('today')+2*24*60*60);
wogsland
  • 9,106
  • 19
  • 57
  • 93
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