cpt-crunchy cpt-crunchy - 1 year ago 73
MySQL Question

PHP echos blank datetime field

Here is my dilemma. I've tried referencing http://php.net/manual/en/datetime.format.php

but thats not helping me or i'm not setting up my code properly.

Here is my code.

$query = "SELECT id,f_name, l_name, phone_1, phone_2, car_name, email, zipcode, ref_name, dateadded ";
$query .= "FROM customers JOIN carriers ON customers.carrier = carriers.car_id ";
$query .= "JOIN referrals ON customers.referral = referrals.ref_id";

$select_customers = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($select_customers)) {
$customer_id = $row['id'];
$c_fname = $row['f_name'];
$c_lname = $row['l_name'];
$c_email = $row['email'];
$c_phone1 = $row['phone_1'];
$c_phone2 = $row['phone_2'];
$car_id = $row['car_id'];
$c_carrier = $row['car_name'];
$c_zip = $row['zipcode'];
$ref_id = $row['ref_id'];
$c_ref = $row['ref_name'];
$c_added = $row['dateadded'];

echo "<tr>";
echo "<td>{$c_fname}</td>";
echo "<td>{$c_lname}</td>";
echo "<td>{$c_email}</td>";
echo "<td>{$c_phone1}</td>";
echo "<td>{$c_phone2}</td>";
echo "<td>{$c_carrier}</td>";
echo "<td>{$c_zip}</td>";
echo "<td>{$c_ref}</td>";
echo "<td>". date_format($c_added, 'd/m/y') ."</td>";
echo "<td><a class='btn btn-danger' href='customers.php?source=edit_customer&id={$customer_id}'>Edit</a></td>";

It will echo out a blank field. The col for the 'dateadded' is set as a datetime and a value of '2016-09-28 10:40:44'. Corrections to my code is appreciated. Thanks.

Answer Source

date_format accepts as first argument

A DateTime object returned by date_create()

Your $c_added is definitely not such object. I advise you to use plain old date function:

echo "<td>". date('d/m/y', strtotime($c_added)) ."</td>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download