如何解決此問題。致命錯誤:帶有消息'DateTime :: __ construct()'的未捕獲異常'Exception':
這是完全錯誤。
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (03-22-2016) at position 0 (0): Unexpected character' in /home/budweiser/public_html/verify.php:13 Stack trace: #0 /home/budweiser/public_html/verify.php(13): DateTime->__construct('03-22-2016') #1 /home/budweiser/public_html/verify.php(35): age('03-10-1991') #2 {main} thrown in /home/budweiser/public_html/verify.php on line 13
我的代碼:
<?php
//created date function
function age($dob){
//echo $dob;
$dob = date("m-d-Y", strtotime($dob));
$date1 = new DateTime($dob);
$date2 = new DateTime(date("m-d-Y"));
$interval = $date1->diff($date2);
return $interval->y;
}
//if a form is submitted do the following
if(array_key_exists("submit", $_POST)){
$mm = $_POST["date_"]["month"];
$dd = $_POST["date_"]["day"];
$yyyy = $_POST["date_"]["year"];
$age = age($mm."-".$dd."-".$yyyy);
//check age here
if($age > 17){
//set cookie here and redirect here if you want
session_start();
$_SESSION["old_enough"] = true;
header('Location: http://91.109.247.179/~buddreambig/index.php');
}else{
//redirect here as well
include("verify-age.html");
}
} else {
include("verify-age.html");
}
?>
更新的代碼
新的錯誤
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1970197019701970-JanJan-ThuThu) at position 7 (0): Double date specification' in /home/budweiser/public_html/verify.php:11 Stack trace: #0 /home/budweiser/public_html/verify.php(11): DateTime->__construct('197019701970197...') #1 /home/budweiser/public_html/verify.php(35): age('--') #2 {main} thrown in /home/budweiser/public_html/verify.php on line 11
<?php
//created date function
function age($dob){
//echo $dob;
$dob = date("YYYY-MM-DD", strtotime($dob));
$date1 = new DateTime($dob);
$date2 = new DateTime("now");
$interval = $date1->diff($date2);
return $interval->y;
}
//if a form is submitted do the following
if(array_key_exists("submit", $_POST)){
$MM = $_POST["date_"]["month"];
$DD = $_POST["date_"]["day"];
$YYYY = $_POST["date_"]["year"];
$age = age($YYYY."-".$MM."-".$DD);
//check age here
if($age > 17){
//set cookie here and redirect here if you want
session_start();
$_SESSION["old_enough"] = true;
header('Location: http://buddreambig.com/');
}else{
//redirect here as well
include("verify-age.html");
}
} else {
include("verify-age.html");
}
?>
請張貼你的代碼,所以我們可以幫助你 –
哦是的。 [這是我的代碼](http://codepad.org/4h8jdVgK) –
請在這裏發佈您的代碼***。 – deceze