2016-03-22 66 views
0

如何解決此問題。致命錯誤:帶有消息'DateTime :: __ construct()'的未捕獲異常'Exception':

this is my site

這是完全錯誤。

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"); 

} 

?> 
+1

請張貼你的代碼,所以我們可以幫助你 –

+0

哦是的。 [這是我的代碼](http://codepad.org/4h8jdVgK) –

+0

請在這裏發佈您的代碼***。 – deceze

回答

0

用以下替換age()函數。 Read more original post

function age($dob){ 
    //echo $dob; 
    $date1 = new DateTime($dob); 
    $date2 = new DateTime("now"); 
    $interval = $date1->diff($date2); 
    return $interval->y; 
} 
+0

感謝您的回覆。我試過你的解決方案,但它仍然給出相同的解決方案。 –

+0

我改變了$ date2 = new DateTime(date(「m-d-Y」));到$ date2 = new DateTime(「now」); 而且我還將日期順序dd-mm-yyyy倒轉爲YYYY-MM-DD 仍然存在,我遇到了同樣的錯誤。 [see here](http://codepad.org/bjGmH5FI) –

+0

刪除'$ dob = date(「YYYY-MM-DD」,strtotime($ dob));'正如我在我的回答中提到的那樣。 –

0

現在的問題得到解決。

我剛剛刪除了我的功能的第一行

謝謝大家。

+0

請用正確的代碼更新答案。你準確刪除了哪一行? –

+0

@Sameer我已經做到了,請參閱我最近的更新。再次感謝 –

相關問題