Date_format does not work in code

Asked

Viewed 362 times

-2

After following several instructions and operating guides of Date_format, I preferred to ask for help as I tried everything and could not format the date and time output of the Mysql database. Does anyone know why this function doesn’t work in this code? I need time at exit 29/02/2006 20:20. In my table my date line 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
  • You’re confusing things. The date_format of mysql is one thing, that of php is another.

  • thanks colleague, what should I use in this case then to get a date in the database the way I need?

2 answers

3


There is an error in your code.

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

Then change this snippet 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

  • then I understood why, I only need to hit this call, I had an error right after the flame, is it right to do so? $searchComentario = mysql_query("SELECT * FROM comments WHERE identificacao = '1' AND moderacao ='nao' ORDER BY data DESC "); $searchComentario .= mysql_query("SELECT *, DATE_FORMAT(data, ’d/m/Y') AS data_formatted FROM comments");

  • I had an error following this code that posted Warning: mysql_fetch_array() expects Parameter 1 to be Resource

2

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

mysql_fetch_array($buscaComentario, MYSQL_ASSOC);

Basically returns an associative array, see more here.

With respect to the time format, you want in the format dd/mm/yyyy hh:mm, similar to the previous answer add only:

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

See a complete reference to the function here

The reason I imagine you’re being negatively (I’m not sure) is because you’re using the extension MYSQL for manipulation of mysql databases. Since it is in disuse, since there are other options such as mysqli and Pdo. Also the MYSQL extension has been removed from PHP version 7. So it would be advisable to migrate to mysqli (MYSQL-like functions). A brief tutorial.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.