Data types in SQL Server 2012 and HTML forms with PHP: date and monetary values

2

How can I put a mask in an HTML form in the date and monetary value fields, then send that data to the database without having to convert the strings to types date and money ?

I explain:

I have a form with date mask in DD/MM/AAAA format, however, my SQL Server 2012 database (BD) uses the AAAA-MM-DD format. When I submit the form for PHP to insert into the DB, the data is sent in the form of string (eg: '31 / 10/2012 '), so I need to do a conversion:

$data = '31/10/2012';
$data_formatada = str_replace('/', '-', $data);
echo date('Y-m-d', strtotime($data_formatada));

The other case is money type data.

The form mask is 9.999.999.999.999,99 and the database has the format 999999999999.99 . When I submit the form for PHP to do the insertion, I have to delete the points and replace the comma by point to only then make insertion of the data in the database.

Question: Is there a more efficient way to insert date and monetary values into the database than the ones I have described?

    
asked by anonymous 08.12.2015 / 10:54

3 answers

3

If there is a more practical way to do this, use the specialized functions for this, do not treat the situation as a text formatting problem.

For dates use the DateTime class, it has the createFromFormat () method that receives a date in format you want and return a DateTime object, use the format () method to change the date format of d/m/Y to Y-m-d

$data =  DateTime::createFromFormat('d/m/Y', '31/12/2015');
echo $data->format('Y-m-d');

For monetary values use the internationalization library Intl is available since php5.3, enable it by php.ini. parseCurrency () , receives a string in currency format and removes all formatting. formatCurrency () does the opposite process from a number returns a string with the currency value applied.

$arr=array('R$530.077,99','R$31.459,89','R$2.899,39','R$600,51','R$13,00','R$9,00','R$0,25');
$formatter = new NumberFormatter('pt_BR',  NumberFormatter::CURRENCY);
foreach($arr as $item){
    echo  $formatter->parseCurrency($item, $valor_puro) . '<br>';
}
    
08.12.2015 / 11:43
3

SQL Server does not have any format, it saves data, all databases work like this. Format is something the application should use. It may even be the application and use a format when doing a query . But storage is something else. If you look in the file you will see that it is not even this format that you think you have. Obviously if you do a query it will render somehow more readable by a human and this is a standard format. Then there is a conversion already. And you need to do another one whenever you want a specific format. You can not get away from it.

PHP has a few options and you can use whatever is most convenient. But you do not have to worry about efficiency. What has to be done, needs to be done. The cost is low and a senseless micro-optimization, especially in PHP.

    
08.12.2015 / 11:47
1

Using HTML5, but without IE support:

<input type="tel" required="required" maxlength="15" 
name="valor" pattern="([0-9]{1,3}\.)?[0-9]{1,3},[0-9]{2}$" />
    
08.12.2015 / 13:15