0

In sql datbase when date datatype is used it stores date in yyyy-mm-dd format is there any way to change it to dd-mm-yyyy? as i want to display date in dd-mm-yyyy format. One way or another would be helpful.

0

4 Answers 4

2

You can either change the column type to VARCHAR(10) and format it to your liking on insertion or preferably you can format it with php after retrieval like:

$date = date('d-m-Y', strtotime($date));
Sign up to request clarification or add additional context in comments.

Comments

2

You can use MySQL's built in DATE_FORMAT function in the query:

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

OR

SELECT DATE_FORMAT(datefield, GET_FORMAT(DATE,'EUR')); 

If you use GET_FORMAT(DATE,'EUR') it will use . instead of - as the separator though.

1 Comment

This is probably the best solution... though to say for sure would depend on workflow and preference.
1

I prefer to save your date in UNIX format so you can use

date("<IDENTIFER>", $timestampUNIX);

to make a timestamp in any format you want. For identifers use http://php.net/manual/en/function.date.php

For this you have to change your database-colum-type to INT.

1 Comment

Though this is perfectly acceptable it does make it hard for a human to make heads or tails of the data without converting it.
-1

I think that there is no problem if it will be stored in yyyy-mm-dd format, but during output you can use date function - >

date("d-m-Y", strtotime($datefrombase)); 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.