如何将MySQL日期时间转换为不同的日期格式?

时间:2016-08-13 18:57:23

标签: php mysql datetime

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

所以我有这个代码从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>"; 

    }

提前多多谢谢你!

1 个答案:

答案 0 :(得分:1)

您可以检查此代码

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