How to get next week's date based on weekdays in current array

Hello,

I researched a lot and tried many times but I was not successful.

Example
Today is the day of the week 4
Today today date is 2024-04-18

$weekdays = array(4); // If there is today of the week in the series, give today's date.
// Output: 2024-04-18
$weekdays = array(1,3,6); // If today's week does not exist in the series, give the date of the day in the series after today.
// Output: 2024-04-20

Thank you from now

If there is not a later day in the array, do you want to go to the following week? For example, if $weekdays = array(1,3); and today is 4, do you want to use next monday, the 22nd, since it corresponds to the 1 in the array, as the result?

Thank you for the answer
Yes, In such a case, the first number in the array should give the date of the 1st day of the week?
That is, array(2,3). In this case, it should give the dates of 2 days of the next week.

I ran such a code, but I don’t know how accurate or professional the coding is.
It looks like amateur coding

    $haftanin_gunu = array(2,3);

    $haftaningunu=date('N');

    // If the foreach loop does not give any results, return the first value in the array.
    $sonraki_gun = $haftanin_gunu[0];

    foreach($haftanin_gunu as $number) {
        if($number > $haftaningunu) {
            $sonraki_gun = $number;
            break;
        }
    }

               if($sonraki_gun=='1'){
          $h_tarihi=date('d.m.Y', strtotime('noon monday')); // Pazartesi
          }elseif($sonraki_gun=='2'){
          $h_tarihi=date('d.m.Y', strtotime('noon tuesday')); // Salı
          }elseif($sonraki_gun=='3'){
          $h_tarihi=date('d.m.Y', strtotime('noon wednesday')); // Çarşamba
          }elseif($sonraki_gun=='4'){
          $h_tarihi=date('d.m.Y', strtotime('noon thursday')); // Perşembe
          }elseif($sonraki_gun=='5'){
          $h_tarihi=date('d.m.Y', strtotime('noon friday')); // Cuma
          }elseif($sonraki_gun=='6'){
          $h_tarihi=date('d.m.Y', strtotime('noon saturday')); // Cumartesi
          }elseif($sonraki_gun=='7'){
          $h_tarihi=date('d.m.Y', strtotime('noon sunday')); // Pazar
          }

          echo $h_tarihi;

Almost the same -

// define week day number to day name, for the strtotime('next day_name') expression
$days[1] = 'mon';
$days[2] = 'tue';
$days[3] = 'wed';
$days[4] = 'thu';
$days[5] = 'fri';
$days[6] = 'sat';
$days[7] = 'sun';

$weekdays = array(4); // If there is today of the week in the series, give today's date.
// Output: 2024-04-18
$weekdays = array(1,3,6); // If today's week does not exist in the series, give the date of the day in the series after today.
// Output: 2024-04-20
$weekdays = array(2,3);
// 2024-04-23

// get today's week day number (1-7)
$wd = intval(date('N'));

// if today's week day number is in the array, use today's date
if(in_array($wd,$weekdays))
{
	$date = date('Y-m-d');
}
else
{
	// find the next week day number and use it's date
	$found = false;
	foreach($weekdays as $day)
	{
		// if a day > today is found
		if($day > $wd)
		{
			$found = true;
			// exit the loop, $day is the found entry
			break;
		}
	}
	if(!$found)
	{
		// a day > today not found, use the first entry
		$day = $weekdays[0];
	}
	echo "Found: $day<br>";
	$date = date('Y-m-d',strtotime("next ".$days[$day]));
}

echo $date;
1 Like

Thank you very much,

I think your coding is more professional than my coding. :+1: :+1: :+1:

Sponsor our Newsletter | Privacy Policy | Terms of Service