How to show date and time in php

2

How do I show the date and time after the user sends a message?

I created a column named 'date' of type 'DATETIME' in the database.

In php I used the following code to insert the data:

Inserir.php

$sql = "INSERT INTO autoriza (ID, data) VALUES( '$id', 'NOW()')";
$acao_sql = $mysqli->query($sql);

Lista.php

$sql = "SELECT * FROM autoriza ORDER BY ID DESC";
$acao_sql = $mysqli->query($sql);
while ($lista = $acao_sql->fetch_array(MYSQLI_ASSOC)){
       echo "Recado enviado em: ".$lista['data']; 
}

The above result shows: 0000-00-00 00:00:00

    
asked by anonymous 04.05.2014 / 22:59

2 answers

4

Use without the function in NOW() and put id with auto_increment in your table:

Inserir.php

$sql = "INSERT INTO autoriza (data) VALUES(NOW())";
$acao_sql = $mysqli->query($sql);

He is not recording a datetime > correctly because of the single quotes ...

To format a datetime use the < a href="http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format"> date_format , as demonstrated below.

Lista.php

$sql = "SELECT date_format(data, '%d/%m/%Y') as data FROM autoriza ORDER BY ID DESC";
$acao_sql = $mysqli->query($sql);
while ($lista = $acao_sql->fetch_array(MYSQLI_ASSOC)){
       echo "Recado enviado em: ".$lista['data']; 
}

References

05.05.2014 / 00:08
0

It has the function date() native to php.

More information:
link

To register id you can use PRIMARY KEY AUTO INCREMENT

More information: link
link
link

<?php
$data = date("Y-m-d H:i:s"); //formato 2014-05-04 18:00:00
$sql = "INSERT INTO autoriza (data) VALUES('".data."')"; 
$acao_sql = $mysqli->query($sql);

//mostra
$sql = "SELECT * FROM autoriza ORDER BY ID DESC"; 
$acao_sql = $mysqli->query($sql);
while ($lista = $acao_sql->fetch_array(MYSQLI_ASSOC)) { 
 echo $lista['data'];
}
?>
    
04.05.2014 / 23:07