1

I've got the following MySQL query:

<?php
$sql = "SELECT * FROM my_table";
$result = mysql_db_query($DBname,$sql,$link) or die(mysql_error()); 

$rows = array();

while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
 }

 print json_encode($rows);

?>

Which outputs the following JSON format:

 [
  {
   "id":"100",
   "due_date":"2010-08-24 10:00:36"
  }
 ]

My question is: In my MySQL query, how would I be able to first convert/modify one row (in this case, the date),or rather, how would I modify my current query, using something like:

$date = strtotime($r['due_date']);
$new_date = date('j M', $date);

And then JSON encode the results, to return this instead:

[
 {
 "id":"100",
 "due_date":"24 Aug"
  }
]

1 Answer 1

7

You could use MySQL's DATE_FORMAT() to do it.

SELECT id, DATE_FORMAT(due_date_field, "%e %b") as due_date FROM my_table;
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.