首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命错误:带有消息“DateTime::__construct()”的未命名异常“exception”:

致命错误:带有消息“DateTime::__construct()”的未命名异常“exception”:
EN

Stack Overflow用户
提问于 2016-03-22 11:20:50
回答 2查看 2.8K关注 0票数 0

我该如何解决这个问题。

这是我的网站

这是完全的错误。

致命错误:在/home/budweiser/public_html/verify.php:13堆栈跟踪:#0 /home/budweiser/public_html/verify.php:#0 /home/budweiser/public_html/verify.php(13):DateTime->_ exception (‘03-22-2016’) #1 /home/budweiser/public_html/verify.php(35):年龄('03-10-1991') #2 {main}抛入第13行的/home/budweiser/public_html/verify.php

我的代码:

代码语言:javascript
复制
<?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");
}
?>

更新代码

新误差

致命错误:在/home/budweiser/public_html/verify.php:11堆栈跟踪:#0 /home/budweiser/public_html/verify.php:11堆栈跟踪:#0 /home/budweiser/public_html/verify.php(11):日期时间-__构造(‘197019701970197.’) #1 /home/budweiser/public_html/verify.php(35):年龄(‘-’) #2 {main}抛入第11行的/home/budweiser/public_html/verify.php

代码语言:javascript
复制
<?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");

}

?>
EN

回答 2

Stack Overflow用户

发布于 2016-03-22 11:35:23

age()函数替换为下面。阅读更多原创文章

代码语言:javascript
复制
function age($dob){
    //echo $dob;
    $date1 = new DateTime($dob);
    $date2 = new DateTime("now");
    $interval = $date1->diff($date2);
    return $interval->y;
}
票数 0
EN

Stack Overflow用户

发布于 2016-03-22 13:27:02

这个问题现在已经解决了。

我刚删除了函数的第一行

谢谢大家。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36153046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档