Inserting dates in php with PDO

Asked

Viewed 636 times

0

I am having trouble inserting a date in the bank with PDO. I have a mirror class in my table in the bank:

class PaginaEntity {
private $id;
private $data;

public function __construct(){
    $this->data = date('d-m-y h:i:s A');
//gets and sethers

The class to insert:

class PaginaDao {
    public function inserirPagina(PaginaEntity $paginaEntity) {
        $conexao = new PDOUtil();
        $insert = $conexao->getStance()->prepare("INSERT INTO tabela_teste(data) VALUES(:data)");
        $insert->bindValue(":data", $paginaEntity->getData());
        $insert->execute();
    }
}

And I have the test form:

include_once '../entity/PaginaEntity.php';
include_once '../dao/PaginaDao.php';
include_once '../configs/PDOUtil.php';

if(isset($_GET["acao"])) {
    $id = $_POST["id"];
    $pagina = new PaginaEntity();    
    $daoPagina = new PaginaDao();
    $daoPagina->inserirPagina($pagina);
}
?>

<form method="post" action="testeInsercaoPagina.php?acao=ok">
    <label for="id">id da categoria</label>
    <input type="text" name="id"> <br/><br/> 
    <button type="submit">Gravar</button>    
</form>

What happens is the following mistake: Imagem do erro

  • 1

    From what I could read, date_default_timezone_set('UTC') resolves.

  • even put $this->data = date(’d-m-y h:i:s A', date_default_timezone_set('UTC'); the error no longer exists but does not insert.

  • There is a definition of time zone in php.ini?

  • 1

    Put at the beginning of the code, before using the date functions.

  • Remove 'A' from date creation. Then you can format it with am/pm

  • I configured phpini the Timezone and the error ended but now does not insert the value, remembering that in the bank I put even varchar.

  • Which error appears? It is right to use timestamp for date and time or date for date. You will have sorting and search problems with varchar

  • no error appears, so I put varchar temporarily but I will switch to timestamap the variables are receiving the following values id=1, which is a relationship and the date = 19-04-15 12:47:37

  • in this date format I can leave timestamp even?

  • @rray switched to timestamp and simply entered, why a date did not insert in a varchar or return error? vc know

  • Leave the Y-m-d H:i:s format or create the date with mysql now()

  • I thought I’d leave it like this you think you’d have trouble’d-m-y h:i:s' in future ordinations?

Show 7 more comments

1 answer

0


As the staff passed me in the comments I solved the error as follows, I took the php.ini path in the terminalphp --ini soon after I added the following date.timezone = 'America/Sao_Paulo'p to configure it, then in my database that had a scan as the value to receive a data and changed it to timestamp as @rray had said.

thank you all.

Browser other questions tagged

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