3

I have a column in mysql which stores the dates. So in mysql the dates are stored in Y-M-D format. However on retrieval i want to view change that format to D-M-Y . I read online at a few places and i found the date() in php, Date conversion. But they are generally for timestamp, also in many cases people had recommended to split the variable by - and then changing position.

My question is that, is splitting the variable the only option here or do we have any built in function that does that for us?

1
  • 3
    strtotime and date are exactly what you need for this. Unless you explicity create an obscene time string, the strtotime function should accurately parse your date. You should at least try it. Commented Mar 20, 2012 at 17:00

3 Answers 3

15

this should work for you, check here http://codepad.org/YdZQzgXR

<? $date = '2012-12-30'; echo date('d-m-Y', strtotime($date)); ?> 
Sign up to request clarification or add additional context in comments.

Comments

7

You can do that on the PHP side or on the MySQL side.

On the MySQL side you could use DATE_FORMAT:

SELECT DATE_FORMAT(NOW(), '%d-%m-%Y'); 

or from a field:

SELECT id, DATE_FORMAT(created_at, '%d-%m-%Y') FROM articles; 

On the PHP side you have more tools:

  • use a database abstraction layer (PDO, MDB, etc.)
  • parse the returned time with strptime and then use strftime to format it
  • split as you mentioned
  • ...

Comments

2

Two possibilities: PHP's strtotime() or let MySQL do the job!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.