Click to See Complete Forum and Search --> : MySql date in $row[xx] - reformat ?


cauli
12-06-2006, 11:00 AM
I know I'm missing something but ....
using $query="SELECT * FROM major WHERE reference=$asking";
I collect the data I need.
One element is $row[dateraised] .. which as you might guess is a date which is formatted "2006-12-06".
I want to print out (echo) with a different format e.g. "d-m-Y"...
can't do it !
help please

ShrineDesigns
12-06-2006, 01:57 PM
store dates as integers (column type: int(10) unsigned), then just use php's date() function, example<?php
// ...
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$row['date'] = date('r', $row['date']);
// ...
}
// ...
?>

legendx
12-06-2006, 02:30 PM
ShringDesigns is correct and I would suggest using his method in the future, but if you need to use you're column type I would suggest looking at php.net's strtotime() function:

http://us3.php.net/manual/en/function.strtotime.php

give it any date/time representation and it will give you a unix timestamp format back, which will work nicely with date()

Hope this helps.

NogDog
12-06-2006, 02:52 PM
Just store it as a date field in PHP, that's what it is, there's no reason to store it as a different type. If you want it to be in a different format when you retrieve it, just use MySQL's DATE_FORMAT() function in your query. See http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html for specifics on this and all the other date/time functions in MySQL.

$query = "SELECT *, DATE_FORMAT(dateraised, '%d-%m-%Y') AS dr FROM major WHERE reference=$asking";

The reformatted date will then be available as $row['dr'] when you fetch the query result.