1

こんにちは、php を使用してフォームから取得した日付を変数に保存しました。日付が季節にあるかどうかを確認したい..私が書いたコードは次のとおりです。

$year = 2012;
$month1 = $_POST["month1"];
$date = $_POST["date"];
$result = "{$year}/{$month1}/{$date}";
echo $result;

そしていま

// finding first saturday in febraury
    $saturday = strtotime('First Saturday '.date('F o', mktime(0,0,0, $month, 1, $year)));
    echo "this is the first saturday in the given year:";
    echo date('Y/m/d', $saturday);

 // calculating first 12 weeks after the first saturday
    echo "<br/>";
    $season1 = strtotime ( '+12 week' , $saturday);
    echo "<br/>this is the first season:";
    echo date('Y/m/d', $season1);
    echo "<br/>";

 // calculating the one week gap after the first 12 weeks
    $season2 = strtotime ('+1 week' , $season1);
    echo "<br/>this is the first week break:";
    echo date('Y/m/d', $season2);
    echo "<br/>";

ここで私がする必要があるのは、ユーザーが指定した日付がシーズン1かシーズン2かを確認することです..そうするために私は試しました

if ($result <= $season1)
    {
     echo "League yet to be opened";
    }
    else 
    {
    echo "league 1 is opened";
    }

しかし、状態はここではチェックされていません。同様に、8シーズンでユーザーが入力した日付を確認する必要があります....どんな助けも大歓迎です....事前に感謝..

4

1 に答える 1

0

I'd advice you use such organisation of code and logic

Define current date from POSTed

Don't forget to became sure data is safe

Year, month and date (day) MUST be numerical, for this logic to work.

$year = 2012;
$month1 = $_POST["month1"];
$date = $_POST["date"];

Define your seasons array

$seasons = array(
   1 => array(
              'season_name' => 'first_season',
              'year_start' => 2012, 'year_end' => 2012, 
              'month_start' => 1, 'month_end' => 2, 
              'day_start' => 10, 'day_end' => 20
              ), 
   2 => array(
              'season_name' => 'second_season',
              'year_start' => 2012, 'year_end' => 2012, 
              'month_start' => 2, 'month_end' => 3, 
              'day_start' => 30, 'day_end' => 15
              ) 
 );

Process data to define season

$season_match = array();
foreach($seasons as $season){
   if($year >= $season['year_start']) && if($year <= $season['year_end']){
       if($month >= $season['month_start']) && if($season<= $season['month_end']){
           if($date >= $season['date_start']) && if($season<= $season['date_end']){
               $seasons_match[] = $season['season_name'];
           }
       }
   }
}

Test for errors and echo resulting season

if(count($season_match) == 0){
   echo 'Date is out of any season';
}
esleif(count($season_match) >1){
   echo 'ERROR: Date can be for more then one season at once';
}
else{
   echo $season_match; // This is the name of our season!
}

It's general logic for simple and clear solution of your task, I haven't tested it.

于 2012-05-11T04:16:15.193 に答える