duanan1228 2016-08-13 18:57
浏览 485
已采纳

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

I've been looking all over for a solution to this and simply can't find something similar. Please don't hate me if it turns out to be a duplicate question.

So I have this code that gets a datetime named 'postDate' from a MySQL row in which I have a row with a: 'title' - 'content' - 'postDate'

It is being displayed as (example): 2016-08-13 20:21:10

How can I turn the DATETIME format into something like 13-08-2016 20:21:10 without having to rewrite my code? Or is this simply impossible?

This is how I have coded it:

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

    }

Thank you VERY MUCH in advance!

  • 写回答

1条回答 默认 最新

  • drdyf42880 2016-08-13 19:38
    关注

    You can check this code out

    <?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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示