duanliao5995
2014-04-28 19:45
浏览 179
已采纳

使用php格式化从数据库到表的日期

I have a database full of football fixtures, sorted in descending order. Everything works fine however the dates are in the following format: YYYYMMDD like so - 2014-05-11.

All table contents are selected with:

 $result = mysqli_query($con,"SELECT * FROM Fixtures ORDER BY Date DESC");

Then inserted into the table with the below code:

while($row = mysqli_fetch_array($result))
  {

  echo "<tr>";
  echo "<td>" . $row['Date'] . "</td>";
  echo "<td>" . $row['Home Team'] . "</td>";
  echo "<td>" . $row['Score'] . "</td>";
  echo "<td>" . $row['Away Team'] . "</td>";
  echo "<td>" . $row['Score1'] . "</td>";
  echo "<td>" . $row['Competition'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

How can I make it so these dates appear DDMMYYYY such as 11-05-2014?

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我有一个装满足球场的数据库,按降序排序。 一切正常但日期的格式如下:YYYYMMDD如此 - 2014-05-11。

所有表格内容均选择:

  $ result = mysqli_query($ con,“SELECT * FROM Fixtures ORDER BY Date DESC”  ); 
   
 
 

然后使用以下代码插入表中:

  while($ row = mysqli_fetch_array(  $ result))
 {
 
 echo“&lt; tr&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['日期']。  “&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['主队']。  “&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['得分']。  “&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['客队''。  “&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['Score1']。  “&lt; / td&gt;”; 
 echo“&lt; td&gt;”  。  $ row ['竞赛']。  “&lt; / td&gt;”; 
 echo“&lt; / tr&gt;”; 
} 
echo“&lt; / table&gt;”; 
   
 
 

如何 我可以这样做这些日期出现DDMMYYYY,如11-05-2014?

谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubi1624 2014-04-28 19:50
    已采纳

    Solution:

    echo "<td>" . date('d-m-Y', strtotime($row['Date'])) . "</td>";
    

    Manual

    打赏 评论
  • dpv21589 2014-04-28 19:47

    Use DateTime::createFromFormat

    $date = DateTime::createFromFormat('dmY', $row['Date']);
    echo $date->format('d-m-Y');
    
    打赏 评论
  • dongyuchen9276 2014-04-28 19:52

    Either fetch it properly from the database:

    $query = "SELECT
        ...,
        ...,
        DATE_FORMAT(Date, '%d-%m-%Y') AS Date
    FROM Fixtures
    ORDER BY Date DESC";
    
    $result = mysqli_query($con, $sql);
    

    Or format it in your PHP

    echo date('d-m-Y', $row['Date']);
    
    打赏 评论

相关推荐 更多相似问题