Date_format does not work in code

-2

After following several Date_format instructions and operating guides, I preferred to ask for help because I tried everything and could not format the date and time output of the Mysql database. Does anyone know why this function does not work in this code? I need the time on exit 29/02/2006 20:20. In my table my date row is of type datetime.

<?php 
        // aqui inicia a busca de comentarios dentro do banco de dados.

        require 'conexao.php';

            $buscaComentario = mysql_query("SELECT * FROM comentarios WHERE identificacao = '1' AND moderacao ='nao' ORDER BY data DESC ");

                while ($lista = mysql_fetch_array($buscaComentario)) {


                    $nome =         $lista['nome'];
                    $site =         $lista['site'];
                    $comentario =   $lista['comentario'];
                    $avatar =       $lista['avatar'];
                    $data =         $lista['data'];


                    DATE_FORMAT('data','%d/%c/%Y %H:%i:%s');
                    date_default_timezone_set('America/Sao_paulo');



                    echo "
                        <div id='comentario'>
                            <img src='uploads/$avatar' width='80'></img>        
                            <p><strong> $nome</strong></p>
                            <p>$comentario $site </p>
                            <span><strong> $data</strong></span>
                        </div>

                        ";
        }
        ?>
    <hr/>
    <?php
    
asked by anonymous 29.02.2016 / 21:52

2 answers

3

There is an error in your code.

You are trying to format the date by php, looking at your code, but by your intention I realize that you want to format by Mysql .

Then change this portion of your query to:

SELECT *, DATE_FORMAT(data, 'd/m/Y') AS data_formatada FROM comentarios

So just use $lista['data_formatada'] to access the formatted date

    
29.02.2016 / 21:59
2

The problem is in the mysql_fetch_array () function, it receives two parameters, the first one is the variable that received the result of its query, and the second the type of array to be returned. Try this:

mysql_fetch_array($buscaComentario, MYSQL_ASSOC);

Basically returns an associative array, see more here .

Regarding the time format, you want the format dd / mm / yyyy hh: mm, similar to the previous response add only:

SELECT *, DATE_FORMAT(data, 'd/m/Y H:i') AS data_formatada FROM comentarios

See a full reference here

The reason I imagine you are being denied (not sure) is because you are using the MYSQL extension for manipulating mysql databases. Since it is deprecated, there are other options like mysqli and pdo . In addition the MYSQL extension has been removed from version 7 of PHP. So it would be advisable for you to migrate to mysqli (functions similar to MYSQL). A brief tutorial .

    
29.02.2016 / 22:35