0

I have following query

 SELECT  str_to_date(`created_at`,'%Y-%m-%d %H:%i:%s') AS `created_at 
 FROM `categories`

If I run this query on PHPMyadmin it's working fine. and gives result

2014-09-15 11:59:55  ,  2014-09-15 12:40:12

I am getting Result in JSON Format OutPut is showing like this

2014-09-15T06:29:55.000Z
Mon Sep 15 2014 11:59:55 GMT+0530 (India Standard Time)

I want same output which is in MYSQL. I have datetime in MYSQL. Does anybody help me out

3
  • You are probably fetching the value and sending it as a date object. Send it as a string object instead. Commented Sep 15, 2014 at 8:46
  • I have no idea. can ypu tell me Commented Sep 15, 2014 at 8:49
  • Refer my answer and the SQL Fiddle link. Commented Sep 15, 2014 at 8:56

3 Answers 3

2

Try this one:

SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d %H:%i:%s') AS `created_at` 
FROM `categories`

DATE_FORMAT gives you date as a string.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this

SELECT  DATE_FORMAT(`created_at`,'%Y-%m-%d %H:%i:%s') AS `created_at 
 FROM `categories`

1 Comment

Please refer comment on the post by Hitu
1

STR_TO_DATE converts the string to a date object.

When the date object is returned via JSON, the result is returned as you have specified.

To avoid this do either of the below,

  1. Convert the Date again to String using DATE_FORMAT and send this string value to your JSON Output.
  2. Since your records are already in the string format that you want, just take the value and pass the string to your json output

Example: Approach 1

SELECT DATE_FORMAT( STR_TO_DATE('18,05,2009','%d,%m,%Y') , '%Y-%m%d %H:%i%s');  

Refer fiddle here: http://sqlfiddle.com/#!2/3285c/3

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.