尝试从mysql数据库使用时间戳时,PHP date_create失败

我只是想找出为什么我不断收到此错误:

Notice: Undefined variable: flsrfc

现在$ flsrfc设置如下:

if (date_create($flstart)) {
   $flsdt = date_create($flstart);
   if ($flsdt) {
      $flsrfc = date_format($flsdt, "Y-m-d\TH:i");
   } else {
      $flsrfc = date_format(date_create(time()), "Y-m-d\TH:i");
   }
}

现在,我遇到的问题是尝试使用我要获取的格式化时间来设置datetime-local输入。这是该代码以及错误说明其来源的代码。我的猜测是date_create函数无法正常工作,但我不知道为什么。

$HTMLOUT .= '<input name="fls" value="'.$flsrfc.'" size="30" type="datetime-local" />';

我确保$ flstart从数据库中获取数据,这是它的代码:

$flstart = mysqli_fetch_row(mysqli_query("SELECT value FROM config WHERE name = 'flstart'"))[0];

我已经阅读了许多关于php的手册页和文档,试图找出原因,我在时间戳上尝试了strtotime,不行,我还尝试将其强制转换为整数,也遇到了同样的问题。有人可以向我指出正确的方向吗?谢谢。

评论
  • ﹏執念
    ﹏執念 回复

    Your issue is that if date_create fails, $flsrfc never gets set. You should rewrite that code as:

    if (($flsdt = date_create($flstart)) !== false) {
        $flsrfc = date_format($flsdt, "Y-m-d\TH:i");
    } 
    else {
        $flsrfc = date("Y-m-d\TH:i");
    }
    

    Note that you can simplify the else portion by just calling date as its default input is the output of time().

    In terms of why date_create is failing, that would depend on the format of the data in your table. If you are storing it as a MySQL date or datetime type, there should be no problem with it.