【问题标题】:Object of class DateTime could not be converted to stringDateTime 类的对象无法转换为字符串
【发布时间】:2012-04-29 21:31:47
【问题描述】:

我在名为Film_Release的字段中有一个表格,其中包含 2012 年 4 月 20 日星期五 格式的字符串值

我正在循环,我想将它们转换为 datetime 并将它们推出到另一个表中。我的第二个表有一个名为Films_Date 的列,格式为DATE。我收到此错误

DateTime 类的对象无法转换为字符串

$dateFromDB = $info['Film_Release'];
$newDate = DateTime::createFromFormat("l dS F Y",$dateFromDB); //( http:php.net/manual/en/datetime.createfromformat.php)

然后我通过插入命令将$newdate插入到表中。

为什么会出现这样的错误?

【问题讨论】:

    标签: php mysql datetime insert


    【解决方案1】:
    $Date = $row['Received_date']->format('d/m/Y');
    

    然后它从数据库中给定日期对象

    【讨论】:

      【解决方案2】:

      如果您使用 Symfony 的 Twig 模板,您可以使用经典的 {{object.date_attribute.format('d/m/Y')}} 来获取所需的格式化日期。

      【讨论】:

      • 这个作品是给我的,但我只用这个。 .format('d/m/Y') myObject .format('d/m/Y')
      【解决方案3】:

      这有点离题,但我来自谷歌搜索同样的错误。 对我来说,当我从 mssql 数据库中选择 datetime 字段而不是稍后在 php-script 中使用它时,就会出现这个错误。 像这样:

      $SQL="SELECT Created
      FROM test_table";
      
      $stmt = sqlsrv_query($con, $SQL);
      if( $stmt === false ) {
          die( print_r( sqlsrv_errors(), true));
      }
      
      $Row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
      
      
      $SQL="INSERT INTO another_test_table (datetime_field) VALUES ('".$Row['Created']."')";
      $stmt = sqlsrv_query($con, $SQL);
      if( $stmt === false ) {
          die( print_r( sqlsrv_errors(), true));
      }
      

      INSERT 语句出错:Object of class DateTime could not be converted to string

      我意识到您不能只从数据库中选择日期时间:

      SELECT Created FROM test_table
      

      但是您必须为此字段使用 CONVERT:

      SELECT CONVERT(varchar(24),Created) as Created FROM test_table
      

      【讨论】:

      • 使用参数 Kenny,他们就在那里,然后将神奇地从 \DateTime 对象sqlsrv_query($con, "INSERT INTO tablename (dtfield) VALUES(?)", array($datetime)) 进行转换
      【解决方案4】:

      试试这个:

      $Date = $row['valdate']->format('d/m/Y'); // the result will 01/12/2015
      

      注意:$row['valdate'] 它是数据库中的起息日

      【讨论】:

        【解决方案5】:

        使用这个:$newDate = $dateInDB->format('Y-m-d');

        【讨论】:

          【解决方案6】:

          检查以确保有电影上映日期;如果缺少日期,您将无法在非对象上格式化。

          if ($info['Film_Release']){ //check if the date exists
             $dateFromDB = $info['Film_Release'];
             $newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
             $newDate = $newDate->format('d/m/Y'); 
          } else {
             $newDate = "none"; 
          }
          

           $newDate = ($info['Film_Release']) ? DateTime::createFromFormat("l dS F Y", $info['Film_Release'])->format('d/m/Y'): "none" 
          

          【讨论】:

            【解决方案7】:

            您正在尝试将$newdate 插入到您的数据库中。您需要先将其转换为字符串。使用DateTime::format 方法转换回字符串。

            【讨论】:

            • 嗨,我尝试使用 format 但收到错误 - 调用非对象上的成员函数 format()
            【解决方案8】:

            因为$newDateDateTime 类型的对象,而不是字符串。 documentation 是明确的:

            根据指定格式返回新的DateTime对象 格式。

            如果您想从字符串转换为DateTime 再转换回字符串以更改格式,请在末尾调用DateTime::format 以从您的DateTime 中获取格式化字符串。

            $newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
            $newDate = $newDate->format('d/m/Y'); // for example
            

            【讨论】:

            • 嗨,我试过了,我得到了这个错误 - 在非对象上调用成员函数 format()
            • @DIM3NSION:除非createFromFormat 没有返回对象,否则无法使用上面的代码,这意味着它返回了false,因为输入无效。阅读documentation 并修复输入。
            • 我将使用什么类型的字段来插入 $new 日期?我现在可以工作了
            猜你喜欢
            • 1970-01-01
            • 2020-12-25
            • 1970-01-01
            • 1970-01-01
            • 2016-11-02
            • 1970-01-01
            • 1970-01-01
            • 2022-01-05
            相关资源
            最近更新 更多