doujiao6116 2016-11-25 08:23
浏览 46
已采纳

从数据库转换检索的日期时间

I'm trying to send the date to my Android device in JSON format. Also, the time is in 24 hours format. How can I change this into 12 hours format?

PHP

<?php
$sql = "SELECT * from news Order by n_date Desc";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);

$result = mysqli_query($con, $sql);
$response = array();
while ($row = mysqli_fetch_array($result)) {
    array_push($response, array("title" => $row[2], "content" => $row[3], "n_date" => $row[4]));
}

echo json_encode(array("news_response" => $response));

mysqli_close($con);
?>

The JSON response:

{"news_response":[{"title":"news","content":"content","n_date":"2016-11-12 10:47:23"}]}

I am trying to convert the 2016-11-12 to something like November 12, 2016 10:47:23. How do I do this?

I couldn't find any example online.

  • 写回答

4条回答 默认 最新

  • dsavz66262 2016-11-25 08:27
    关注

    in PHP:

    echo (new DateTime('2016-11-12 10:47:23'))->format('F d, y GA:i:s');
    

    in MySQL:

    SELECT concat(MONTHNAME(date),' ', DAY(date), ' ', TIME(date)) from news;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化