2

This is my string value 20180421

I want to convert this value into 2018/04/21

here my code

$querya="SELECT STR_TO_DATE('$date','%m/%d/%Y');";

but i does not change my string

1 Answer 1

1

Your format is already in ISO format, so you can just use date() for the conversion:

select date(?)

You can also use str_to_date() but you need to use the correct format:

$querya = "SELECT STR_TO_DATE(?, '%Y%m%d')";

Note the use of ?. This is a parameter placeholder and indicates that you should pass the value in as a parameter rather than as a variable.

I should point out that 2018/04/21 is a particular format. Dates are stored in an internal format. To get a particular output format, you need to convert them to strings. Here is one solution:

select date_format(date(?), '%Y/%m/%d')

The date() function is optional, because MySQL will correctly convert this string to a date, so you can also use:

select date_format(?, '%Y/%m/%d')
Sign up to request clarification or add additional context in comments.

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.