user1114409 user1114409 - 1 year ago 54
MySQL Question

Extracting only date from datetime field (mysql) and assigning it to php variable

I am unable to assign only date to a php variable from

field of a MySQL table. I am using PHP 4, I know how to do it in PHP 5.

Can someone help as to what is missing from the below php code.

$dc = mysql_query("SELECT * FROM INVHDR WHERE Invdate BETWEEN '2011-04-01' AND '2012-03-31'");
while ($dc2 = mysql_fetch_assoc($dc)) {
$invno = $dc2['Invno']; // Here Invno is a datetime field type
$invdat3 = $dc2['Invdate'];
$date_array = explode("-", $invdat3);
$invdat = sprintf('%02d/%02d/%4d', $date_array[2], $date_array[1], $date_array[0]);

Answer Source

If you just want to display the date portion, you could use the date function:

echo date("d/m/Y", strtotime("2012-12-24 12:13:14"));