以正确的格式显示日期php sql

时间:2015-05-25 10:04:09

标签: php sql date

我在显示以正确格式从sql数据库表中获取的日期时遇到了一个小问题。目前它显示为yyyy-mm-dd,而我希望它显示为dd.mm.yyyy或dd / mm / yyyy。

以下是我正在使用的代码:

<?php

function select_races($sql) {

$day = (int) (isset($_GET['day']) ? $_GET['day'] : date('d')); 
$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
$year =  (int) (isset($_GET['year']) ? $_GET['year'] : date('Y'));

$date = $day.".".$month.".".$year;

include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    echo "<div class='races' id='$year' style='display:block'>
     <table>\n<th colspan='5'>Cycling Season 2015 </th>\n
            <tr id='information'>\n
                <th id='date'>Date</th>\n
                <th id='race'>Race</th>\n
                <th id='route'>Route</th>\n
                <th id='info'>Entry</th>\n
                <th id='rizultz'>Results</th>";
}

while ($row = $result->fetch_assoc()) {
    if ($row['race_activityID'] == 3) {
        if ($row['infoID'] != "0"){
            echo "<tr class='racedetails' id='cancelled' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'>".$row["infoID"]."</td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        } else {
            echo "<tr class='racedetails' id='cancelled' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'>N/A</td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        }
    } 
        else if ($row['race_activityID'] == 2) {
            if ($row['infoID'] != "0") {
            echo "<tr class='racedetails' id='nextrace' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
            }
            else {
            echo "<tr class='racedetails' id='nextrace' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'>N/A</td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        }
        }
        else {
            if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
            echo "<tr class='racedetails' id='race' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
            <td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
            }
            else if ($row['infoID'] != "0") {
            echo "<tr class='racedetails' id='race' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
            }
            else if ($row['resultID'] != "0") {
            echo "<tr class='racedetails' id='race' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'>N/A</td>\n
            <td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
            }
            else {
            echo "<tr class='racedetails' id='race' >\n
            <td class='dark' id='date'>".$row["date"]."</td>\n
            <td class='light' id='race'>".$row["title"]."</td>\n
            <td class='dark' id='route'>".$row["route"]."</td>\n
            <td class='light' id='info'>N/A</td>\n
            <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        }
        }

}
echo "</table>\n
        </div>\n";
 }

 ?>

我用来发送数据的代码:

<?php select_races("SELECT date, title, route, race_activityID, infoID, resultID FROM races"); ?>

5 个答案:

答案 0 :(得分:1)

使用DateTime界面 -

$date = new DateTime('2015-05-25'); // $row["date"] = '2015-05-25';

echo $date->format('d.m.Y');

答案 1 :(得分:0)

尝试在SQL中执行:

SELECT  DATE_FORMAT(date,'%d.%m.%Y') as formatteddate, , title, route, race_activityID, infoID, resultID FROM races;

答案 2 :(得分:0)

试试此代码

date('d.m.Y', strtotime($row["date"]));// it will show date in 24.05.2015 format
date('d-m-Y', strtotime($row["date"]));// it will show date in 24-05-2015 format

答案 3 :(得分:0)

您甚至可以尝试在SQL中格式化日期

<?php select_races("SELECT DATE_FORMAT(date,'%d.%m.%Y') as date, title, route, race_activityID, infoID, resultID FROM races"); ?>

或显示格式(更好的解决方案)

$date = new DateTime($row['date']);
$formatted_date = $date->format('d.m.Y');
if ($row['race_activityID'] == 3) {
    if ($row['infoID'] != "0"){
    echo "<tr class='racedetails' id='cancelled' >\n
    <td class='dark' id='date'>".$formatted_date."</td>\n
    <td class='light' id='race'>".$row["title"]."</td>\n
    <td class='dark' id='route'>".$row["route"]."</td>\n
    <td class='light' id='info'>".$row["infoID"]."</td>\n
    <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
    } else {
    echo "<tr class='racedetails' id='cancelled' >\n
    <td class='dark' id='date'>".$formatted_date."</td>\n
    <td class='light' id='race'>".$row["title"]."</td>\n
    <td class='dark' id='route'>".$row["route"]."</td>\n
    <td class='light' id='info'>N/A</td>\n
    <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
    }
} 
    else if ($row['race_activityID'] == 2) {
        if ($row['infoID'] != "0") {
      echo "<tr class='racedetails' id='nextrace' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
      <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        } else {
      echo "<tr class='racedetails' id='nextrace' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'>N/A</td>\n
      <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
  }
    }
    else {
        if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
      echo "<tr class='racedetails' id='race' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
      <td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
        } else if ($row['infoID'] != "0") {
      echo "<tr class='racedetails' id='race' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
      <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
        }
        else if ($row['resultID'] != "0") {
      echo "<tr class='racedetails' id='race' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'>N/A</td>\n
      <td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
        }
        else {
      echo "<tr class='racedetails' id='race' >\n
      <td class='dark' id='date'>".$formatted_date."</td>\n
      <td class='light' id='race'>".$row["title"]."</td>\n
      <td class='dark' id='route'>".$row["route"]."</td>\n
      <td class='light' id='info'>N/A</td>\n
      <td class='dark' id='rizults'>N/A</td>\n</tr>\n";
  }
    }

答案 4 :(得分:0)

使用MySQL DATE_FORMAT()

<?php select_races("SELECT DATE_FORMAT(date,'%d.%m.%Y') as `date`, title, route, race_activityID, infoID, resultID FROM races"); ?>