0
我已經通過搜索,但我已經嘗試的答案似乎並沒有解決我的問題。 我有這個簡單的代碼片段,用戶將輸入數字日期和一個月,應用程序將返回相應的星座。檢查日期是否在範圍內的PHP strtotime
$birthdate = $_POST["birthdate"];
$birthmonth = (ucwords(strtolower($_POST["month"])))
//validations here. . .
$tmp = $birthmonth . " " . $birthdate;
$tmp2 = date_create_from_format('M j', $tmp);
$formatted_dob = date_format($tmp2, 'm-d-Y');
$dob = strtotime($formatted_dob);
echo $formatted_dob;
if ($dob >= strtotime('01-20-2016') && $dob <= strtotime('02-18-2016')) {
echo "Aquarius";
} elseif ($dob >= strtotime('02-19-2016') && $dob <= strtotime('03-20-2016')){
echo "Pisces";
}
的if-else
塊之外的那些東西echo
工作正常,但是,如果我輸入的25的值和二月(其稍後結果02-25-2016
),它總是輸出Aquarius
。你如何比較兩個strtotime
值?
我試過使用DateTime
對象,但它只給了我一個錯誤,這是另一回事。提前致謝。
的'date_create'工作正常,謝謝。但它不適用於其他 - 如果我有12個條件過濾那種驗證。 @LeeBalino – Cyval
@Cyval更新了我的答案。請檢查它是否適用於您。 –
我也試過你的代碼,只是改變你在strtotime上輸入的日期,將它從m-d-Y更改爲Y-m-d,因爲它是導致錯誤的原因之一。 @Cyval –