Inserting date in American standard

Asked

Viewed 231 times

0

I have a function to import data from a txt document, the layout that I use to import this data is delimited by semicolon, I have the following columns (number, data_inclusion, sender, currency, value, status). The problem is time to insert the date, because in txt I use the Brazilian standard dd/mm/yyyy and in mysql it does not accept this standard, as I do to convert this 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>

2 answers

1


I removed $itens[1] and inserted date("Y-m-d H:i:s", strtotime($itens[1])) that is making the conversion to 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>

0

You can use the function date():

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

Reference: PHP documentation.

  • The date format DD/MM/YY (26/01/2018) is not recognised by the function strtotime (accepted formats) and the output format is not what is accepted by mysql (YY-MM-DD). See for example: https://3v4l.org/tH9op

Browser other questions tagged

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