简体   繁体   English

查询中的日期格式更改

[英]Date Format Change in Query

I am using PHP JSON as API in android.我在 android 中使用 PHP JSON 作为 API。 I am taking news from MYSQL data with below code.我正在使用以下代码从 MYSQL 数据中获取新闻。 Its working fine but date format is showing as YYYY-MM-DD.它工作正常,但日期格式显示为YYYY-MM-DD。 But I want get it like DD-MM-YYYY.但我想得到它像DD-MM-YYYY。 I have searched lot of but not found any solution.我已经搜索了很多但没有找到任何解决方案。 Can anybody here can solve my issue ?这里有人可以解决我的问题吗?

 <?php $response = array(); require 'db.php'; $query = "SELECT * FROM news where order by id desc"; $result = mysqli_query($conn,$query); if (mysqli_num_rows($result) > 0) { $response["news"] = array(); while ($row = $result->fetch_assoc()) { $news= array(); $news["id"] = $row["id"]; $news["title"] = $row["title"]; $news["description"] = $row["description"]; $news["news_date"] = $row["news_date"]; array_push($response["news"], $news); } $response["success"] = 1; // echoing JSON response //echo json_encode($response); echo json_encode($response['news']); } else { $response["success"] = 0; echo json_encode($response); }

Thank谢谢

改变这一行

   $news["news_date"] = date("d-m-Y" ,strtotime  ($row["news_date"]) ); 

You can modify your SQL a bit to format the date to DD-MM-YYYY using date_format() like this:您可以稍微修改您的 SQL 以使用date_format()将日期格式化为 DD-MM-YYYY,如下所示:

select
    id,
    title,
    description,
    date_format(news_date,'%d-%m-%Y') news_date
from 
    news
order by
    id desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM