markmchugh
asked on
how to format dates using php + mysql
Hi,
I have a mysql database, and using php, i want to format the date in european format, which is dd/mm/yyyy
the dates are being inputed to the database using the curdate function, the current output of a date is
2007-11-27 00:00:00
i need it to be 27/11/2007
thanks
I have a mysql database, and using php, i want to format the date in european format, which is dd/mm/yyyy
the dates are being inputed to the database using the curdate function, the current output of a date is
2007-11-27 00:00:00
i need it to be 27/11/2007
thanks
ASKER
can i do it in php?
Why you want to do it in PHP? Anyway take a look at this http://www.php.net/manual/en/ref.datetime.php
Change the datetime field to a date field, this will eliminate the 00:00:00 portion of the field.
Then I would format it using PHP, it's slightly faster.
$sql = "select date_field from table;";
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)) {
$formated_date = date("d/m/Y", strtotime($row['date_field ']);
echo $formated_date;
}
Then I would format it using PHP, it's slightly faster.
$sql = "select date_field from table;";
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)) {
$formated_date = date("d/m/Y", strtotime($row['date_field
echo $formated_date;
}
@nplib
Is it faster to format the date in PHP rather than MySQL? How?
Is it faster to format the date in PHP rather than MySQL? How?
>can i do it in php?
yes. strtotime() will get the time value from the string yyyy-mm-dd.
then, with the date() function, you can format it to the format you want
yes. strtotime() will get the time value from the string yyyy-mm-dd.
then, with the date() function, you can format it to the format you want
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@wasifg
just the way sql severs process commands.
I'm not to sure why it is, but I have a table that has 3000+ records, and when I do it with date_format() it takes 0.05 seconds to return all, and when I do it without it takes 0.005 seconds, although both real fast, doing the php way is marginably faster, that's why I said slightly.
just the way sql severs process commands.
I'm not to sure why it is, but I have a table that has 3000+ records, and when I do it with date_format() it takes 0.05 seconds to return all, and when I do it without it takes 0.005 seconds, although both real fast, doing the php way is marginably faster, that's why I said slightly.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format