2013-06-30 155 views
-2

我有一個查詢在html頁面中顯示即將到來的日期。我試圖讓日期顯示爲dd/mm/yyyy。完整的代碼如下。 「date1」是我想要顯示的日期。在php中更改日期的格式

<?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); 
?> 

感謝所有幫助

回答

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>"; 
    ... 
+0

完善三江源 – user2536134

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>"; 
1

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