首页 > 解决方案 > php 显示 3 个日期:下周三、周六、周日(包括今天)

问题描述

目前我写了很长的代码:

if ($today_day == "Wednesday"){
  //show today date, show next saturday date, show next sunday date
}elseif ($today_day == "Saturday"){
  //show today date, show next Sunday date, show next Wednesday date
}elseif ($today_day == "Sunday"){
  //show today date, show next Wednesday date, show next Saturday date
}else{
  //show next Wednesday date, show next Saturday date, show next Sunday date
}

对此还有其他解决方案吗?我想缩短代码。

标签: php

解决方案


在这里尝试以下解决方案:

if ($today_day == "Wednesday"){
  //show today date, show next saturday date, show next sunday date
   $today = date('Y-m-d');
   $next_saturday = date('Y-m-d',strtotime("next saturday + 1 week"));
   $next_sunday = date('Y-m-d',strtotime("next sunday + 1 week"));
}
elseif ($today_day == "Saturday"){
  //show today date, show next Sunday date, show next Wednesday date
   $today = date('Y-m-d');
   $next_wednesday = date('Y-m-d',strtotime("next wednesday + 1 week"));
   $next_sunday = date('Y-m-d',strtotime("next sunday + 1 week"));
}
elseif ($today_day == "Sunday"){
  //show today date, show next Wednesday date, show next Saturday date
   $today = date('Y-m-d');
   $next_wednesday = date('Y-m-d',strtotime("next wednesday + 1 week"));
   $next_saturday = date('Y-m-d',strtotime("next saturday + 1 week"));
}
else{
  //show next Wednesday date, show next Saturday date, show next Sunday date
   $next_saturday = date('Y-m-d',strtotime("next saturday + 1 week"));
   $next_wednesday = date('Y-m-d',strtotime("next wednesday + 1 week"));
   $next_sunday = date('Y-m-d',strtotime("next sunday + 1 week"));
}

推荐阅读