Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct():

how do I resolve this issue.

[this is my site]http://buddreambig.com/index.php)

this is the complete error.

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

Presumably this happens once someone has logged in? Given that we don’t have a login id and password we can’t see the error, but it might help if we can see verify.php

yes, you are absolutely right.

this is my code

thanks.

Shouldn’t this

    $date2 = new DateTime(date("m-d-Y"));

be just

    $date2 = new DateTime("now");

I’ve not tried it, but the doc suggests the date passed in should be in YYYY-MM-DD format. Maybe the leading ‘0’ for a year is unexpected.

1 Like

hi, thanks for responding.

yes you are right, I changed $date2 = new DateTime(date("m-d-Y")); to $date2 = new DateTime("now");

and I also reversed the date sequence dd-mm-yyyy to YYYY-MM-DD

still, I am getting the same error.

see here. http://codepad.org/bjGmH5FI

So you need to check what’s in the string you’re passing into the function. This extracted code works perfectly for me:

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

$age = age("1973-11-02");
echo $age;
?>

Looking at the error you posted, it seems the age is being passed correctly, but I’m not sure what else it could be.

hmm, I am really out of ideas. thanks anyway

Echo $dob inside the function, post what it says.

nothing different. same error.

No, I meant post the value of the $dob when it runs inside the function. What is being passed in?

I did this in code, and the error is still the same.

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

I meant

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

I know the error will be the same, but seeing the value of the variable might give a clue why. Because it’s a fatal error, execution stops at that point so putting it at the end, after the exception is being thrown, means it never gets that far.

I did the changes and this what I got.

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('10-10-1982') #2 {main} thrown in /home/budweiser/public_html/verify.php on line 13

OK, that doesn’t look like anything has changed - that is, the dates still seem to be in the “wrong” format, as MM-DD-YYYY rather than YYYY-MM-DD. And if you added the echo() at the start of the function, didn’t it echo the value of $dob as well? Don’t take this the wrong way, but are you uploading the changes into the correct place? If you just add an echo in the main code to just show some text, does it appear?

hi, you are absolutely right, I was editing on a wrong one.

this is right code.

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

}

?>`

and this the error I am getting.

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

Lost the first line of the function, there’s no need to do that extra process on the $dob variable. Just pass the string you started with into the Datetime construction.

Maybe…

thanks for responding. I am not using timezone.

thanks a mil. It solved the issue.

Even if formatting the incoming DoB was necessary for what you’re doing, the time format you use in that first line isn’t correct. If you look at the first error, it shows that it has somehow got a date of “1970197019701970-JanJan-ThuThu” out of that line, and that’s because “YYYY-MM-DD” isn’t proper syntax for the date() function. However the line isn’t required at all, remove it and it should work better. Like the sample code I posted in post #6.

2 Likes