PHP转换日期然后JSON编码

Dod*_*nas 1 php json

我有以下MySQL查询:

<?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);

?>
Run Code Online (Sandbox Code Playgroud)

其中输出以下JSON格式:

 [
  {
   "id":"100",
   "due_date":"2010-08-24 10:00:36"
  }
 ]
Run Code Online (Sandbox Code Playgroud)

我的问题是:在我的MySQL查询中,我如何能够首先转换/修改一行(在本例中为日期),或者更确切地说,我将如何修改当前查询,使用类似于:

$date = strtotime($r['due_date']);
$new_date = date('j M', $date);
Run Code Online (Sandbox Code Playgroud)

然后JSON对结果进行编码,然后返回:

[
 {
 "id":"100",
 "due_date":"24 Aug"
  }
]
Run Code Online (Sandbox Code Playgroud)

Amb*_*ber 7

你可以使用MySQL DATE_FORMAT()来做到这一点.

SELECT id, DATE_FORMAT(due_date_field, "%e %b") as due_date FROM my_table;
Run Code Online (Sandbox Code Playgroud)