将从数据库Y-M-D获取的标准日期格式更改为D-M-Y

时间:2015-02-12 11:50:44

标签: php database

我可以使用php从数据库中获取数据。我有一个领域是" DATE"但是当我得到显示的日期时,格式为YEAR / MONTH / DAY。这是因为价值的标准格式" DATE"是否有任何其他选项或方式我可以用来以DAY / MONTH / YEAR格式显示日期?谢谢

我是MYSQL和php

here is the code:
$query = "SELECT * FROM trip";
$result = mysql_query($query);
echo "<table >"; 
while($row = mysql_fetch_array($result)){   
if ($row['Day'] == date("Y-m-d"))
{
echo '<tr class="today">';
}
else {echo "<tr>";
}
echo "<td>" . $row['Day'] . "</td>

}
echo "</table>"; 
mysql_close(); ?>

-----新代码----

 include('includes/connection.php');

    // $connection = mysql_connect('localhost', 'root', ''); 

$sql = <<<SQL
    SELECT *
    FROM `trip`

SQL;

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
    echo "<table >"; 
                    {   
                    if ($row['Day'] == date("Y-m-d"))
                    {
                    echo '<tr class="today">';
                    }
                    else {
                    echo "<tr>";
                    }
                    echo "<td>" . date('d/m/Y', strtotime($row['Day'])) . "</td>

                    <td>" . $row['Country'] . "</td>
                    <td>" . $row['Place'] . "</td>

                    </tr>";  //$row['index'] the index here is a field name
                    }
                    echo "</table>"; //Close the table in HTML

}

1 个答案:

答案 0 :(得分:1)

你可以用php格式化它。

例如: 你可以做点什么 日期(&#39; d / m / Y&#39;,strtotime($ database_date)); http://php.net/manual/de/function.date.php

基于您的代码 - 这将完成工作:

date('d/m/Y', strtotime($row['Day']))

如果需要根据用户区域设置格式化日期,可以使用strftime。 http://php.net/manual/de/function.strftime.php

$query = "SELECT * FROM trip";
$result = mysql_query($query);
echo "<table >"; 
while($row = mysql_fetch_array($result)){   
    if ($row['Day'] == date("Y-m-d"))
    {
         echo '<tr class="today">';
    }
    else {
         echo "<tr>";
    }
    echo "<td>" . date('d/m/Y', strtotime($row['Day'])) . "</td>";
}

echo "</table>"; 
mysql_close(); ?>