Data php Pdo format

Asked

Viewed 477 times

1

Input:

<html>  
 <form action="../controller/progPrecontrole.php" method="POST" onsubmit="return valid();">
   <input type="hidden" name="id" value="<?php echo $id; ?>"/>
   <p>Data Saída:</p>
   <input type="text" maxlength="10" onkeypress="return dateMask(this, event);" id="saida" name="saida" value="<?php echo $DtSaida; ?>"/>
   <input type="submit" class="button" value="Aplicar"/>
   <a class="secondary button" href="programacao.php"> Voltar </a>
 </form>
</html>

Functions:

<?php
class ProgDAO{

private $conn;

public function __construct($connection) {
    $this->conn = $connection;
}

public function listar($id){
        $prog = new Prog();
        $stmt = $this->conn->prepare(
            'SELECT * FROM GTCLogist WHERE ID = :ID'
        );
        $stmt->bindValue(':ID', $id, PDO::PARAM_INT);
        $stmt->execute();
        if($stmt) {
            while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                $prog->setid($row->ID);
                $prog->setsaida(date('d/m/Y', strtotime($row->DtSaida)));
            }
        }
    return $prog;
}

public function editar(Prog $prog){
    $this->conn->beginTransaction();
    try {
        $stmt = $this->conn->prepare(
            'UPDATE GTCLogist SET DsMotorista = :DsMotorista, 
                DtSaida = :DtSaida WHERE ID = :ID'
        );
        $stmt->bindValue(':ID', $prog->getid(), PDO::PARAM_INT);
        $stmt->bindValue(':DtSaida', $prog->getsaida(), PDO::PARAM_INT);
        $stmt->execute();
        $this->conn->commit();
    }
    catch(Exception $e) {
        $this->conn->rollback();
    }
}
?>

The problem is that when I edit this input it does not save, the database format is American default date (yyyy,mm,dd), I used the list function to print inside the dd/mm/yyyyyy input because when I edit it saves mm/dd/yyyyyy, if you put a value greater than 12 in the first field of the date it already ignores thinking it is one month. I need you to save in dd/mm/yyyy format.

  • 1

    I think that solves => http://answall.com/a/5887/91

1 answer

1


Code right as it turned out:

$data_formatada = DateTime::createFromFormat('d/m/Y', $DtSaida);

Input:

<input type="date" maxlength="10" onkeypress="return dateMask(this, event);" id="saida" name="saida" value="<?php echo $data_formatada->format('Y-m-d'); ?>"/>

Browser other questions tagged

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