Object of class DateTime could not be converted to string

后端 未结 7 665
灰色年华
灰色年华 2020-11-27 19:16

I have a table with string values in the format of Friday 20th April 2012 in a field called Film_Release

I am looping through and i want to convert

7条回答
  •  情歌与酒
    2020-11-27 19:49

    Check to make sure there is a film release date; if the date is missing you will not be able to format on a non-object.

    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"; 
    }
    

    or

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

提交回复
热议问题