有效工作日的日期(0,1,2,...,6)作为加载页面时文本框的值

help please, I have a textbox, and a number nday,

nday is the number of the day of week:

0 for sunday, 1 for monday,..., 6 for saturday

i want to make a code using javascript or jquery or php, to set the value of the textbox on load of the page equal to the first date of the current month,

example:

if nday=3, that means it's a wednesday, I want the date of the first wednesday of the current month to be the shown on the textbox, but i have to make sure that this date will not be < date of today

<input type="text" name="d">

</div>
$nday = 3;
$today = date('d') + 1;
$total_month_days = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));

$day = null;
for($i = $today; $i <= $total_month_days; $i++)
{
    $date = mktime(0, 0, 0, date("m"), $i, date("Y"));
    $current_nday = date("w", $date);
    if($current_nday == $nday)
    {
        $day = $i;
        break;
    }
}

if(!empty($day)) echo date("Y")."-".date("m")."-".$day;

The algo that I can think of is :

function find_date():
   if (value of n) < (today's day of the week):
     then say k = (today's day of week) - n
     so the date on the next nth day would be:
     answer = (today's date) + (7-k)
     // 7 because it is the total number of days in a week
   else if (value of n) > (today's day of the week):
     it would simply be
     answer = (today's date) + (value of n)
   else if (value of n) == (today's day of the week):
     answer = today's date
   return answer

So say for example n=3 (meaning Wednesday) and today being Thursday (that is day 4 of the week), so if we need to find out what would be the date on the next Wednesday, as per our algo above:

if n<3 (which is true in our example)
so k = 4 - 3 = 1
So date on next Wednesday would be:
(Today's date = 6th April 2017) + (7-1) 
which is equal to :

6th April + 6 = April 12th 2017

Now the code implementation of the above should not be too hard to figure out. (Not sure if there are any libraries that already do this):