Object of class DateTime could not be converted to string

后端 未结 7 667
灰色年华
灰色年华 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:57

    Because $newDate is an object of type DateTime, not a string. The documentation is explicit:

    Returns new DateTime object formatted according to the specified format.

    If you want to convert from a string to DateTime back to string to change the format, call DateTime::format at the end to get a formatted string out of your DateTime.

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

提交回复
热议问题