如何将MySQL的datetime转换成不同的日期格式


How to turn MySQL datetime into different date format?

我一直在寻找解决方案,根本找不到类似的东西。如果这是一个重复的问题,请不要恨我。

所以我有这个代码,从MySQL行中获得一个名为'postDate'的日期时间,其中我有一行:'title' - 'content' - 'postDate'

显示为(示例):2016-08-13 20:21:10

我怎样才能把DATETIME格式变成像13-08-2016 20:21:10这样的东西而不必重写我的代码?还是说这根本不可能?

我是这样写的:

        //Query the database for the selected posts
    $table = 'page04posts';
    $sql = "SELECT * FROM page04posts";
    $results = $jdb->select($sql);
    //Fetch our results into an associative array
    $results = mysql_fetch_assoc( $results );
    // loop through results of database query, displaying them in the table 
    for ($i = $start; $i < $end; $i++)
    {
        // make sure that PHP doesn't try to show results that don't exist
        if ($i == $total_results) { break; }
        // echo out the contents of each row into a div
        // display data in div
        echo "<div class='container1' style='margin-bottom:2%;'>";
        echo '<div class="h1"><h1>' . mysql_result($result, $i, 'title') . '</h1></div>';
        echo '<p>' . mysql_result($result, $i, 'content') . '</p>';
        echo '<p>' . mysql_result($result, $i, 'postDate') . '</p>'; // this gives the date (example): 2016-08-13 20:21:10
        // close div                   
        echo "</div>"; 
    }

提前谢谢你!

您可以查看这段代码

<?php
$dateFormat= '2016-08-13 20:21:10'; //from your example
$date = new DateTime($dateFormat);
echo $date->format('d-m-Y H:i:s'); // 13-08-2016 20:21:10
?>