I'm trying to get created_at
field from database table but it's throwing the below error:
Internally stored date/time/timestamp value could not be converted to DateTime: '
Cook
' [wrapped: DateTime::__construct(): Failed to parse time string (Cook
) at position 0 (<): Unexpected character]
Code for fetching date:
foreach ($pager->getResults() as $row):
?>
<tr>
<td><?php echo $row->getTitle() ?></td>
<td><?php echo ($row->getStatus()) ? 'Active' : 'Inactive' ?></td>
<td><?php echo date(sfConfig::get('app_display_alternate_format_for_date'), strtotime($row->getCreatedAt())) ?></td>
<td>
</td>
</tr>
Config for Date:
'app_display_alternate_format_for_date' => 'M-d-Y'
Date save in DB format:
2017-09-15 08:08:02
Base getCreatedAt function at BaseModel:
public function getCreatedAt($format = 'Y-m-d H:i:s')
{
if ($this->created_at === null) {
return null;
}
if ($this->created_at === '0000-00-00 00:00:00') {
// while technically this is not a default value of NULL,
// this seems to be closest in meaning.
return null;
} else {
try {
$dt = new DateTime($this->created_at);
} catch (Exception $x) {
throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->created_at, true), $x);
}
}
if ($format === null) {
// Because propel.useDateTimeClass is TRUE, we return a DateTime object.
return $dt;
} elseif (strpos($format, '%') !== false) {
return strftime($format, $dt->format('U'));
} else {
return $dt->format($format);
}
}
First, the exception looks like it finds the string Cook
in the field.
I'd var_dump($this->created_at);exit;
a line before the new DateTime($this->created_at)
to exclude this possibility.
Once you're confident this is not the case, try giving the format of the string. Instead of new DateTime()
use:
$dt = date_create_from_format('Y-m-d H:i:s', $this->created_at);
or, better:
$dt = date_create_from_format($format, $this->created_at);