[英]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.