1

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?

3 Answers 3

7

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);
1

You can reformat date in your sql fetch request

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

0

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.