Nikitas
Nikitas

Reputation: 1003

Change date format on Fetch mysql

For Example.

In the field "date_account_created" It stores a date with format "2011-12-21 03:34:21"

And that's ok...

But when i fetch this in PHP i need to:

1) Do not print the clock "03:34:21"

2) I want to Print DD-MM-YYYY instead of YYYY-MM-DD

Is this thing possible?

Upvotes: 1

Views: 1688

Answers (3)

user436939
user436939

Reputation:

You can reformat date in your sql fetch request

DATE_FORMAT(date, '%d-%m-%Y') AS new_date

Upvotes: 1

Derk Arts
Derk Arts

Reputation: 3460

Read the manual: http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_date-format

mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
        -> 'Sunday October 2009'

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270617

You don't change it as it's being fetched, but rather when querying it by using DATE_FORMAT()

SELECT
  DATE_FORMAT(date_account_created, '%d-%m-%Y') AS date_account_created
FROM table...

That is, unless you will be needing to use the date in PHP in multiple different formats. In that case, it may be simpler to select it as a UNIX timestamp, which can be natively manipulated in PHP without needing to convert via strtotime(). This is recommended only if you intend to use the date in multiple different formats in your PHP application code. If you're using only one format, it's simpler to use DATE_FORMAT() in the query as above.

SELECT UNIX_TIMESTAMP(date_account_created) AS date_account_created FROM table...

Then manipulate in php:

// Already fetched into $date_account_created
echo date("d-m-Y", $date_account_created);

Upvotes: 7

Related Questions