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.
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