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

如何将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!

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

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

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

如何在不必重写代码的情况下将DATETIME格式转换为13-08-2016 20:21:10? 或者这根本不可能? p>

这就是我编码的方式: p>

  //查询所选帖子的数据库
  
 $ table ='page04posts'; 
 $ sql =“SELECT * FROM page04posts”; 
 $ results = $ jdb-&gt; select($ sql); 
 
 //将我们的结果提取到关联数组中 
 
 $ results = mysql_fetch_assoc($ results); 
 
 //循环数据库查询结果,将它们显示在表
 
中($ i = $ start; $ i&lt; $ end;  $ i ++)
 {
 //确保PHP不会尝试显示不存在的结果
 
 if($ i == $ total_results){break;  } 
 
 //将每行的内容回显到div 
 //显示div中的数据
 
 echo“&lt; div class ='container1'style ='margin-bottom:2%;'  &gt;“; 
 echo'&lt; div class =”h1“&gt;&lt; h1&gt;'  。  mysql_result($ result,$ i,'title')。  '&lt; / h1&gt;&lt; / div&gt;'; 
 echo'&lt; p&gt;'  。  mysql_result($ result,$ i,'content')。  '&lt; / p&gt;'; 
 echo'&lt; p&gt;'  。  mysql_result($ result,$ i,'postDate')。  '&LT; / p为H.';  //这给出了日期(示例):2016-08-13 20:21:10 
 
 //关闭div 
 
 echo“&lt; / div&gt;”;  
 
} 
  code>  pre> 
 
 

提前多多谢谢您! p> div>

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