PHP Convert Date Then JSON Encode
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 que开发者_开发问答ry, 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"
}
]
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;
精彩评论