Format date and monetary value to save to database

Asked

Viewed 792 times

3

I’m extracting data from a file .xlsx. converting them into .csv and showing it on screen. So far so good, however, I want to play this data in a database, using PHP, but for this I need to convert the data, because the way they are, they will not enter the database, see:

10-01-15 SAI 87,059.56 UVA SAFRA 2015 - APB

I need to format the date 10-01-15 and the value 87,059.56, the rest of the data is quiet. get out - UVA SAFRA 2015 - APB

Would I have to play so even on the bench, and then catch them, convert the date and value and play on another table?

Follows the code:

if (isset($_POST['pega'])) {

include_once("PHPExcel/Classes/PHPExcel.php");

$uploadDir = "uploadFile/";

$uploadfile = $uploadDir . $_FILES['arquivo']['name'];

if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $uploadfile)) {
    echo "Arquivo pego com sucesso";
    echo "<br><br>";
}else{
    echo "Não foi possível pegar arquivo";
    echo "<br><br>";
}

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadfile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$csvFileName = str_replace('.xlsx', '.csv', $uploadfile);
$objWriter->save($csvFileName);
if (($handle = fopen($csvFileName, "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        $num = count($data);
        for ($c = 0; $c < $num; $c++) {
            echo $data[$c]." ";
        }
        echo "<br />\n";
    }
    fclose($handle);
}
}

I was able to separate the dice, but I can’t play for the bench, I’m using this:

        $pdo = conectar();
        $insereDados=$pdo->prepare("INSERT INTO dadosImportados (data, tipo, valor, descricao) VALUES (?,?,?,?)");
        $insereDados->bindValue(0, $data[0]); 
        $insereDados->bindValue(1, $data[1]); 
        $insereDados->bindValue(2, $data[2]); 
        $insereDados->bindValue(3, $data[3]); 

        $insereDados->execute();

But this message appears on the screen: "Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[HY093]: Invalid Parameter number: Columns/Parameters are 1-based' in /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importFile.php:68 Stack trace: #0 /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importArchive.php(68): Pdostatement->bindValue(0, '10-01-15') #1 {main} thrown in /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importFile.php on line 68"

  • Format date for format YYYY-DD-MM ? What type of column will hold the monetary value?

  • Puts the code you use to grab the xlsx and play on the screen. It will help to come up with a suggestion.

  • 10-01-15 SAI 87,059.56 UVA SAFRA 2015 - APB Does this come as a csv value? Will that comma after 87 not bother you?

  • Yes, yyyy-dd-mm, the column will be of the type Date

  • No, each dice comes in an array value echo $data[$c]." ";

  • It generates a . csv, but is being displayed on the screen the Becco data.

  • I deleted the comment from above, now that you posted the code got better the question.

  • The question itself is basically how to format the date and the monetary value? So, everything else is unnecessary. Or has something more than that?

  • That’s right Daniel Omine... Actually the other data are not unnecessary, but the date and value, should be adjusted to go to the bank.

  • Some personal idea?

  • i meant that in the context of the question it was unnecessary to put so many details. Simply ask how to form date and currency.

  • Our guy! There are some who say to detail, specify the question better and now you say it is not necessary.

  • You must have good sense in discerning what "detailing a question" is. Just put what is essential. In your case, for example, you only want to know how to format the date and currency. Therefore, everything else that is not related to this is out of context. But it is not enough to just ask and wait. I also recommend to show what you tried to do to solve because this is not a free technical support center where you play the codes and, in a touch of magic come solutions.

  • You can choose a correct answer?

  • the answers didn’t help you? if they didn’t help, what’s missing to help?

Show 10 more comments

2 answers

1

Whereas the original date is in the dd-mm-yy format (2-digit day, month and year), the year of which is within the 21st century (2001~2100) and, also considering that it is using Mysql database, the string should be format in the standard ISO 8601

$str = '10-01-15';
$arr = explode('-', $str);
$arr = array_reverse($arr);
echo '20'.implode('-', $arr); // Resulta em 2015-01-10

If the column type in the database is datetime, add time.

Since there is no time specification in the code you posted in the original question, you can add time with zero.

echo '20'.implode('-', $arr).' 00:00:00'; // Resultará em 2015-01-10 00:00:00

For the currency, it depends on how the column is defined in the database. The recommendation is the type decimal(14,6), whether or not working with coins of full value and regardless of the visual format have decimal values of 2 digits.

$str = '87,059.56';
echo str_replace(',', '', $str); // Resulta em 87059.56

If you set the database column to decimal(14,6), the value will be saved as 87059.560000.

When reading this column, to present with 2 houses and formatted with comma, in the visual pattern of Brazil, use the function number_format():

echo number_format('87059.560000', 2, ',', '.'); // Resulta em 87.059,56

Note that I did not adapt the answer to the code posted in the question as this is your work.

Here you have the information needed to proceed and implement for your case or use for other miscellaneous cases.

  • 1

    Daniel, the data type for the date will be Date, and the Decimal value(12,2). There is no mystery. E sorry, but I found very complex its explanation hehehe Another thing, it’s a bit more complicated, because all the data is being printed on this: for ($c = 0; $c < $num; $c++) { echo $data[$c]." " ; }

  • -1 for the "class that gives negative to random" :p I missed the AP ++1 for the detailed answer :)

  • +1 for the good answer.

1

Your question was:

Would I have to play like that on the bench, and then catch them, convert the date and value and play in another table?

Transform your data the way you want and after that you should throw it in your bank. You must not pass that responsibility on to the bank, because it already has other responsibilities. Treat the dice and play to your bank only once, without going and coming from table. Treat the dice before, and then save them.

  • It would really be nice to take everything to the bank in a temporary table then makes the treatment and puts in the final table.

  • Yes durtto, but how could I treat that date 10-01-15 and that figure 87,059.56? For they are coming in an array and it is complicated to catch them and treat.

Browser other questions tagged

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