[英]Change standard date format fetched from database Y-M-D into D-M-Y
我能夠使用php從數據庫中獲取數據。 我有一個字段“ DATE”,但是當我得到顯示的日期時,格式為YEAR / MONTH / DAY。 這是因為值“ DATE”的標准格式是該格式,還是我可以使用其他任何選項或方式以DAY / MONTH / YEAR格式顯示日期? 謝謝
我是usinf 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
}
您可以使用php格式化。
例如:您可以執行類似date('d / m / Y',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(); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.