Date_format() and date_create() error

I have the following lines of code below -

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

But they seem to be conflicting creating the following errors

PHP Warning: date_format() expects parameter 1 to be DateTime, boolean given in
PHP Warning: date_create() expects parameter 1 to be string, object given in

Can you echo out your $data[‘Contract_Start’] to make sure that it’s what you think it is?

Hi sure yes,

I was just about to post the code below and see if that was correct

$ContractStart = new DateTime($data['Contract_Start']);
$ContractEnd = new DateTime($data['Contract_End']);

echo "<td width='80' class='tableData2'>".date_format($ContractStart, 'd-m-Y')."</td>";
echo "<td width='80' class='tableData2'>".date_format($ContractEnd, 'd-m-Y')."</td>";

OK I’ve got myself a fatal error now as a result of the code below.

$ContractStart1 = new DateTime($data['Contract_Start']);
$ContractEnd1 = new DateTime($data['Contract_End']);
$RenewalDate1 = new DateTime($data['Renewal_Date']);

echo "<td width='80' class='tableData'>".date_format($ContractStart1, 'd-m-Y')."</td>";
echo "<td width='80' class='tableData'>".date_format($ContractEnd1, 'd-m-Y')."</td>";
echo "<td width='80' class='tableData'>".date_format($RenewalDate1, 'd-m-Y')."</td>";

PHP 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:912
Stack trace:
#0 \CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\contracts.php(912): DateTime->__construct(Object(DateTime))
#1 {main}
thrown in \CSFFILES11\WEBSITES\live\new_checksafetyfirst\en\csfintranet\contracts.php on line 912

As @kreut said above, you need to var_dump your three variables to see what’s wrong. Which is line 912, in the latest code? Error message suggests that they’re not the expected strings.

chances are that your variables are already DateTime objects.

I’m trying to echo that line out but I get a fatal error every time, which is odd in itself. Will work it out and post the result

He is saying to put the following code above what I have listed above.

var_dump($data['Contract_Start']);
var_dump($data['Contract_End']);
var_dump($data['Renewal_Date']);

Hi cpradio, ye I finally got what he meant, blimey my heads not on it today. This is the output

object(DateTime)#6 (3) { ["date"]=> string(19) "2005-09-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Dublin" }

For var_dump($data[‘Contract_Start’]);

So it is already a DateTime object and thus you don’t need

$ContractStart1 = new DateTime($data['Contract_Start']);
$ContractEnd1 = new DateTime($data['Contract_End']);
$RenewalDate1 = new DateTime($data['Renewal_Date']);

You can simply use $data['Contract_Start] in replacement of $ContractStart1

I thought that and tried it earlier, but when I leave the line as

echo "<td width='80' class='tableData'>".$data["Contract_Start"]."</td>";

It creates a fatal error, and the table stops outputting at that point.

No, you have to leave the date_format($data['Contract_Start'], 'd-m-Y') part. Or use $data['Contract_Start']->format('d-m-Y')

1 Like

Thank you guys

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