0

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.

cpt-crunchy
  • 391
  • 4
  • 23

5 Answers5

1

There are many ways to do it, another one to previous answers:

echo "<td>". date_format(new DateTime($c_added), 'd/m/y') ."</td>";
jakub wrona
  • 2,212
  • 17
  • 17
1

Please try to replace:

echo "<td>". date_format($c_added, 'd/m/y') ."</td>";

in your code With:

echo '<td>' . gmdate('d/m/y', (int)$c_added['0']) . '</td>';

Or

echo '<td>' . date('d/m/y',strtotime($c_added)) . '</td>';

Just a note: Using "<td>" is slower, using '<td>' is faster

jagb
  • 912
  • 11
  • 26
  • useful information. Didn't know about the difference. How 'much' faster is it ? – cpt-crunchy Oct 01 '16 at 21:13
  • You might want to do benchmark to see the difference, please read [this Question/Answer](http://stackoverflow.com/questions/8291366/how-to-benchmark-efficiency-of-php-script), it depends on what you do and how you'd like to do it.. http://www.phpbench.com has some standard test results but if you want to know exact info on your project you should benchmark the code by yourself – jagb Oct 01 '16 at 23:47
0

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>";
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

date_format function formats DateTime datatype while $c_added is a String.

dy'sback
  • 115
  • 5
0

RTM. date_format does not take string as argument.

You should use something else, like

date("d/m/Y", strtotime('2016-09-28 10:40:44'));

Which returns "28-09-2016"

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DevilaN
  • 1,317
  • 10
  • 21