Stefan Felder
Stefan Felder

Reputation: 23

Proplems with formatting DateTime in foreach

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

<?php foreach ($CLT as $Arr2) : ?>
          <tr>
            <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>
          </tr>
                <?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...

Upvotes: 2

Views: 45

Answers (1)

Umer Abbas
Umer Abbas

Reputation: 1876

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');

Upvotes: 1

Related Questions