Convert date format [duplicate]

Posted on

Question :

I need to convert the data format of the fields from my form to write to my bank. As the data is coming from the form by array I am not able to capture them and convert before writing.
The date field is on my form marked ” ” and masked in the format 00/00/0000 from there when I need to convert it to 0000 / 00/00 .
The value field is in my form as ” value [] ” with currency mask 1,000,00 but I need to convert it to 1000.00 .
I have tried the conversion before and also inside the loop but it gives error.

$valorparc = $_POST['valor'];
    $data_vctoparcela = $_POST['vencimento'];
    $quant_linhas = count($_POST['valor']);
   for ($i=0; $i<$quant_linhas; $i++) {
       $data = $data_vctoparcela[$i];
       $valorp = $valorparc[$i];
 mysql_query("INSERT INTO lc_parcelas (ID,cod_cliente,id_fatura,valor_parcela,data_vcto,parcela)
            VALUES('".$idEMP."','".$cod_cliente."','".$id_fatura."','".$valorp."','".$data."','".$i."')") or die (mysql_error());


Answer :

You could do this as follows to format the fields:


//Formatar data
$data = '11/20/2015';

$dataParts = array_reverse(explode('/', $data));
//echo implode('/', $dataParts);//01/11/2015
//Para gravação no banco de dados certamente deve ser dessa forma
echo implode('-', $dataParts);//01-11-2015

echo '<br>';

//Formatar moeda
$morney = '1.000,00';
$newMorney = str_replace(array('.', ','), array('', '.'), $morney);
echo number_format($newMorney, 2, '.', '');//1000.00

Your code could look like this:

$valorparc = $_POST['valor'];
$data_vctoparcela = $_POST['vencimento'];
$quant_linhas = count($_POST['valor']);

for ($i = 0; $i < $quant_linhas; $i++) {

  $dataParts = array_reverse(explode('/', $data_vctoparcela[$i]));
  $data = implode('-', $dataParts);//01/11/2015

  $newMorney = str_replace(array('.', ','), array('', '.'), $valorparc[$i]);
  $valorp = number_format($newMorney, 2, '.', '');//1000.00

  mysql_query("INSERT INTO lc_parcelas (ID, cod_cliente, id_fatura, valor_parcela, data_vcto, parcela) VALUES('$idEMP','$cod_cliente','$id_fatura','$valorp','$data','$i')")
  or die (mysql_error());



Here you have php is good 🙂
you make an explode the original string then you contact everything and you form a single string or in java script.

    $str = 'one,two,three,four';
    // zero limit
    print "<br>";
    // positive limit
    print "<br>";
    // negative limit 


Leave a Reply

Your email address will not be published. Required fields are marked *