Find the position in the week of the date the user entered (date("w", $USER_TIMESTAMP))
Then, calculate the numeric distance between the day you want and the one entendes by user.
Suppose the user entered 1 (Monday) and you want 5 (Friday), 5 - 1 = 4 (wanted day - user day = y)
But suppose the user entered 6 (Saturday) and you want 5... 5 - 6 = -1 ! No problem ! 5 + 7 = 12, 12 - 6 = 6 (wanted day + number of days in week = x, x - user dat = y)
Now, what do we do with y...
First, we'll use mktime :
$user_timestamp = mktime(0, 0, 0, $user_month, $user_day, $user_year);
Second, since there are 86 400 seconds in one day, we will multiply it by the number of days between the two days (y)...
$wanted_timestamp = $user_timestamp + (86400 * $y);
$y is what we found above... 12 - 6 = 6, ...
That will give you a new timestamp as wanted !
Let's put that together...
<?
$user_timestamp = mktime(0, 0, 0, $user_month, $user_day, $user_year);
$user_weekday = date("w", $user_timestamp);
$wanted_weekday = 5;
$diff = $wanted_weekday - $user_weekday;
if($diff == 0) {
$diff = 7;
} elseif($diff < 0) {
$diff = $wanted_weekday + 7 - $user_weekday;
}
$new_timestamp = $user_timestamp + (86400 * $diff);
$new_month = date("n", $new_timestamp);
$new_day = date("j", $new_timestamp);
$new_year = date("Y", $new_timestamp);
echo "New date is $new_day/$new_month/$new_year";
?>
$user_month, $user_day and $user_year are the infos entered by the user... from a form for example...
I hope it will help you ! It works perfectly on my computer !