在php中更改日期格式

时间:2013-06-30 16:59:20

标签: php

我有一个查询,在html页面中显示即将到来的日期。我想把日期显示为dd / mm / yyyy。完整代码如下。 "日期1"是我想要显示的日期。

<?php
$con=mysqli_connect("localhost","root","","mentertraining");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$query = "SELECT `coursedates`.`coursedate_id`,`courses`.`course_title`,`courses`.`no_of_days`,`category`.`category_name`,`coursedates`.`date1` FROM coursedates "
    . " LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` "
    . " LEFT JOIN `mentertraining`.`category` ON `courses`.`category_id` = `category`.`category_id` LIMIT 0, 5 ";

$result = mysqli_query($con,$query);
    echo "<table cellspacing='1'><tr bgcolor='#fe0075'><th>Course Title</th><th>Course Date</th></tr>";

while($row    = mysqli_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td>" . $row['course_title'] . "</td>";
  echo "<td>" . $row['date1'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

如果日期是字符串格式,您可以date function使用echo date('d/m/Y', strtotime($row['date1']);

答案 1 :(得分:0)

while($row = mysqli_fetch_assoc($result))
{
    //I suppose, you have date in mysql fomat
    $date = new DateTime($row['date1']);
    $row['date1'] = $date->format('d/m/Y');

    echo "<tr>";  
    ...

答案 2 :(得分:0)

    echo "<table cellspacing='1'><tr bgcolor='#fe0075'><th>Course Title</th><th>Course Date</th></tr>";

while($row    = mysqli_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td>" . $row['course_title'] . "</td>";
  echo "<td>" ."d-m-Y", strtotime($row['date1'])  . "</td>";
  echo "</tr>";
  }
echo "</table>";