2017-01-15 21 views
-1

私はアンドロイドのAPIとしてPHP JSONを使用しています。私は以下のコードでMYSQLデータからニュースを取っています。正常に動作していますが、日付形式はYYYY-MM-DDです。しかし、私はDD-MM-YYYYのようにしたい。私はたくさんの検索をしましたが、解決策は見つかりませんでした。誰も私の問題を解決できるのですか?日付形式の変更点

<?php 
 
$response = array(); 
 
require 'db.php'; 
 

 
$query = "SELECT * FROM news where order by id desc"; 
 
\t 
 

 
    $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); 
 
}

あなたはこのようなdate_format()を使用してDD-MM-YYYYに日付をフォーマットするために、あなたのSQLビットを変更することができます

答えて

1

変更するには、この行を

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

これは正解で、私に必要な結果を与えました。 –

0

ありがとう:

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

こんにちは! "警告":mysqli_num_rows()は、パラメータ1がmysqli_result、myroot/getNews.phpで指定された行10を返します。 {"success":0}、10行目がif(mysqli_num_rows() $ result)> 0){ –