2015-10-26 11 views
2

因此,對於我的學校作業之一,我一直在用戶輸入他們的生日後出現問題。我必須用2種方式迴應用戶。 1)如果日期等於他們的生日,他們會收到一個好消息,2)如果日期不等於他們的生日,那麼他們會得到不同的消息。如果日期等於他們的生日,如何回顯給用戶?

由於某種原因,我的if語句不工作...幫助?

這裏的if語句........

<?php  

//if statement for birthday 
$month = $_POST['month']; 
$year = $_POST['year']; 
$day = $_POST['day']; 

$date = $year ."-". $month ."-".$day; 

$date = date("Y-m-d",strtotime($date)); 

if(date('m-d') == date('m-d', $date)) { 
    // today is users birthday. show any message you want here. 
    echo "<p>Happy Birthday $firstname!</p>\n"; 
} else { 
    echo "<p>You were born $date.</p>\n"; 
} 

?> 

而且......這裏的下拉列表....

<?php 
      $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

      //1950 is standard year thing for most websites I've noticed 
      $yearFrom = 1950; // The first year included in the drop-down for years 


      // $yearFrom = date("Y")-80; 
      // Using this line instead, gives a dynamic range of years, always 80 years 
      // Echo out all years via a for-loop 
      //<select> IS the dropdown box 
      echo "<select name=\"year\" id=\"year\">"; 
      //less than or equal to current year 
      for ($yearFrom; $yearFrom <= date("Y"); $yearFrom++) { 
        // Each $yearFrom represents a year, always incremented by 1 year 
        //echos out every year that we put present....... 
        echo "<option value=\"$yearFrom\">$yearFrom</option>"; 
      } 
      echo "</select>"; 


      // Echo out all months from the array $months 
      echo "<select name=\"month\" id=\"month\">"; 
      foreach($months as $key=>$value) { 
        // $key is the index of the array, starting at 0 
        $numericMonth = $key + 1; 
        echo "<option value=\"$numericMonth\">$value</option>"; 
      } 
      echo "</select>"; 


      // Echo out all days (1-31) via a for-loop 
      echo "<select name=\"day\" id=\"day\">"; 
      for ($i=1; $i <= 31; $i++) { 
        // Each $i represents a numeric value of days, from 1-31 
        echo "<option value=\"$i\">$i</option>"; 
      } 
      echo "</select>"; 

      ?> 

所以......這就是我有..所有的想法和援助非常感謝。 :)

+0

您是否收到錯誤消息或輸出不是你期待什麼? – Lima

+0

@利馬沒有錯誤。它輸出錯誤的信息。 – Sarah

回答

1

觀看演示here

要使用strtotime()並得到準確的結果,首先設置您的日期和時區。

試試這個,

date_default_timezone_set ('Asia/Kolkata');// this leni should be added to use strtotime() 

$date = "date from post value"; 
if (date('m-d', strtotime($date)) == date('m-d')) { 
    echo "<p>Happy Birthday $firstname!</p>\n"; 
} else { 
    echo "<p>You were born $date.</p>\n"; 
} 

觀看演示here

+0

當生日等於日期時,它仍然回顯出其他部分... – Sarah

+0

它的工作原理!謝謝一堆! – Sarah

+0

完成了,完成了!再次感謝! – Sarah

2

嘗試這樣

$date = "Your boirthday date"; 
$dob = date('d-m',strtotime($date));// 
$today = date('d-m'); 
if($dob == $today) 
{ 

    $todayBirthday = "Bithday today"; 


}else{ 
    $noTodayBirthday = "<h2 class='birthdaytoday'>No Birthday </h2>"; 

    } 
0

我修改@Ricky版本略有

$date = $_POST['year'] ."-". $_POST['month'] ."-".$_POST['day']; 
    $dob = date('d-m',strtotime($date)); 
    $today = date('d-m'); 
    if($dob == $today) 
    { 
     echo "<p>Happy Birthday $firstname!</p>\n"; 
    } else 
    { 
     echo "<p>You were born $date.</p>\n"; 
    } 
相關問題