dougong7850 2018-01-02 17:06
浏览 112
已采纳

MySQL数据库和导入日期问题

Okay,so in my mysql database I use a date field. Using PHP I check if the user is 18 years of age or not. Then I try to enter all the form information to my MySQL database to make a user. I keep getting a blank screen (besides my navbar and footer) The user is not being saved into the database, and the error log shows this error: PHP Recoverable fatal error: Object of class DateTime could not be converted to string in ....

$age = checkAge($_POST["birthday"]);
if($age != false) 
{
  $ageSuc = "All Good!";
}
else ....


function checkAge ($data)
{
  $dateObj = new DateTime($data);
  $ageLimit = new DateTime('-18 years');
  if ($dateObj > $ageLimit)
  {
   return false;
  }
  else 
  {
    $dateObj->format('Y-m-d');
    return $dateObj;
  }
}

So the question is, do I need to convert the dateTime Obj into a string before MySQL will accept it? The field is set to hold 'dates' so I thought the date obj would be the same thing? How does one change it to a string.

  • 写回答

2条回答 默认 最新

  • douzengjian1535 2018-01-02 17:09
    关注

    You are returning the DateTime object, because the format function returns a string, it doesn't change the object. Try returning the result of the format function which is the formatted string.

    return $dateObj->format('Y-m-d');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!