php – Proplems with formatting DateTime in foreach-ThrowExceptions

Exception or error:

I have this code here and it worked fine until I tried formatting dates.

<?php foreach ($CLT as $Arr2) : ?>
            <td><?php echo $Arr2['VPNummer'] ?></td>
            <td><?php echo utf8_encode ($Arr2['VPVorname']) ?> <?php echo utf8_encode ($Arr2['VPName']) ?></td>
            <td><?php echo $Arr2['Firma'] ?></td>
            <td><?php echo $Arr2['Vorname'] ?> <?php echo $Arr2['Name'] ?></td>
            <td><?php echo $Arr2['PLZ'] ?> <?php echo $Arr2['Ort'] ?></td>
            <td><?php Dateformat ($Arr2['Datum']); ?></td>
            <td><em class="fa fa-user-edit fa-fw text-muted"></em> <em class="fa fa-minus-circle fa-fw text-muted"></em></td>
                <?php endforeach; ?>

I try to format the dates from Database for example 2020-03-12 11:11:12 -> to 12.03.2020
I am using this external function, because that is often needed in different files and tables.

function Dateformat($datum)
  $newdate = DateTime::createFromFormat('Y-m-d h:i:s', $datum)->format('d.m.Y');
  echo $newdate;

It works for the first entry, then it breaks off.
I also tried to unset $newdate, but no effect.

$CLT = mysqli_query($conn, "SELECT * FROM `InteressentenEmpfehlungen` WHERE `VPID` = 776 AND `Geloescht` = 0 ORDER BY `Datum` DESC");

I am using this SQL Statement, perhaps its better to convert the dates before creating an array…

How to solve:

if you have 24 hours time then it will throw an error. use capital H in the format.

DateTime::createFromFormat('Y-m-d H:i:s', $datum)->format('d.m.Y');

Leave a Reply

Your email address will not be published. Required fields are marked *