Inserting date into the American standard

0

I have a function to import data from a txt document, the layout I use to import this data is delimited by semicolons, I have the following columns (number, date_inclusion, sender, currency, value, situation, status). The problem is the time to insert the date, because in txt I use the Brazilian standard dd / mm / yyyy and in mysql it does not accept this pattern, how do I convert that date to the American standard before running the insert?

My code:

 <?php

    function Inserir($itens, Pdo $pdo){
        $sts = $pdo->prepare("INSERT INTO orpags(numero,data_inclusao,remetente,moeda,valor,situacao,status) VALUES(?,?,?,?,?,?,'1');");
        $sts->bindValue(1, $itens[0], PDO::PARAM_STR);
        $sts->bindValue(2, $itens[1], PDO::PARAM_STR);
        $sts->bindValue(3, $itens[2], PDO::PARAM_STR);
        $sts->bindValue(4, $itens[3], PDO::PARAM_STR);
        $sts->bindValue(5, $itens[4], PDO::PARAM_STR);
        $sts->bindValue(6, $itens[5], PDO::PARAM_STR);
        $sts->execute();
        $sts->closeCursor();
        $sts = NULL;

     /* por ponto e virgula*/
            }
    if (!empty($_FILES['arquivo']))
    {
        $Pdo     = new PDO("mysql:host=localhost;dbname=smoke", "root", "");
        $file    = fopen($_FILES['arquivo']['tmp_name'], 'r');
        while (!feof($file)){
            $linha = fgets($file);          
            $itens = explode(';', $linha);          
            Inserir($itens, $Pdo);
        }
    }


?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Arquivo</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post">
        <input type="file" name="arquivo" id="arquivo">
        <input type="submit" name="enviar" value="Enviar">
    </form>
</body>
</html>
    
asked by anonymous 26.01.2018 / 18:43

2 answers

1

I removed $itens[1] and inserted date("Y-m-d H:i:s", strtotime($itens[1])) that is converting to the international standard.

<?php

function Inserir($itens, Pdo $pdo){
    $sts = $pdo->prepare("INSERT INTO orpags(numero,data_inclusao,remetente,moeda,valor,situacao,status) VALUES(?,?,?,?,?,?,'1');");
    $sts->bindValue(1, $itens[0], PDO::PARAM_STR);
    $sts->bindValue(2,date("Y-m-d H:i:s", strtotime($itens[1])), PDO::PARAM_STR);
    $sts->bindValue(3, $itens[2], PDO::PARAM_STR);
    $sts->bindValue(4, $itens[3], PDO::PARAM_STR);
    $sts->bindValue(5, $itens[4], PDO::PARAM_STR);
    $sts->bindValue(6, $itens[5], PDO::PARAM_STR);
    $sts->execute();
    $sts->closeCursor();
    $sts = NULL;

 /* por ponto e virgula*/
        }
if (!empty($_FILES['arquivo']))
{
    $Pdo     = new PDO("mysql:host=localhost;dbname=smoke", "root", "");
    $file    = fopen($_FILES['arquivo']['tmp_name'], 'r');
    while (!feof($file)){
        $linha = fgets($file);          
        $itens = explode(';', $linha);          
        Inserir($itens, $Pdo);
    }
}


?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Arquivo</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post">
    <input type="file" name="arquivo" id="arquivo">
    <input type="submit" name="enviar" value="Enviar">
   </form>
</body>
</html>
    
26.01.2018 / 19:10
0

You can use the date() function:

date ("YYYY / MM / dd", strtotime ("26/01/2018"));

Reference: PHP Documentation .

    
26.01.2018 / 18:52