如何解决此问题。
这是完整的错误。
致命错误:带有消息的未捕获异常'异常' 'DateTime :: __ construct():无法解析时间字符串(03-22-2016)at 位置0(0):意外的字符'in /home/budweiser/public_html/verify.php:13堆栈追踪:#0 /home/budweiser/public_html/verify.php(13): DateTime-> __ construct('03 -22-2016')#1 /home/budweiser/public_html/verify.php(35):年龄('03 -10-1991')#2 {main}在第13行的/home/budweiser/public_html/verify.php中抛出
我的代码:
<?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");
}
?>
更新代码
新错误
致命错误:带有消息的未捕获异常'异常' 'DateTime :: __ construct():无法解析时间字符串 (1970197019701970-JanJan-ThuThu)位置7(0):双重约会 规范'在/home/budweiser/public_html/verify.php:11堆栈 追踪:#0 /home/budweiser/public_html/verify.php(11): DateTime-&gt; __ construct('197019701970197 ...')#1 /home/budweiser/public_html/verify.php(35):年龄(' - ')#2 {main}抛出 在第11行的/home/budweiser/public_html/verify.php
<?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");
}
?>
答案 0 :(得分: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;
}
答案 1 :(得分:0)
问题现已解决。
我刚删除了我的第一行功能
谢谢大家。