date_format() error in error logs

I have been going through my error logs and I have a date_format() error, and have looked into it and it seemed it was all about adding new DateTime to help, but it made the problem worse.

This is what I had originally -

echo "<td width='80' class='tableData'>".date_format($data['Contract_Start'], 'd/m/y')."</td>";

And this is the error I get as a result - PHP Warning: date_format() expects parameter 1 to be DateTime, null given in

So I looked into it to resolve this issue and tried below

$date1 = new DateTime($data['Contract_Start']);
echo "<td width='80' class='tableData'>".$date1->format($data['Contract_Start'], 'd/m/y'). "";"</td>";  

But then I get a fatal error as below, so its made it worse.

Fatal error: Uncaught exception ‘Exception’ with message ‘DateTime::__construct() expects parameter 1 to be string, object given’ in \CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\contracts.php:903 Stack trace: #0 \CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\contracts.php(903): DateTime->__construct(Object(DateTime)) #1 {main} thrown in

Any ideas

you’ve already got a DateTime object there. no need to create it once again.

Thanks for getting back to me quickly Dormilich, using the 2 lines of code I have posted, could you explain your bit by using my code, sorry.

echo "<td width='80' class='tableData'>".DateTime->construct($data['Contract_Start'], 'd/m/y'). "";"</td>";

This doesn’t work, that’s what I have tried, sorry should have posted it, and didn’t use

$date1 = new DateTime($data['Contract_Start']);

the code I posted was from the error message. it’s not supposed to be actual code.

the second snippet you posted should work, but since I don’t know which line #903 is, I can’t tell what you did wrong there, only why PHP complained about it.

edit: the format() method only accepts the format string (unlike the date_format() function, which is just a wrapper)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.