Convert date format

Asked

Viewed 409 times

4

People, I need to convert the data format of my form fields to save in my database. As the data is coming from the form by array I am not able to capture them and convert before recording. The date field is in my form identified as "maturity[]" and with mask in the format 00/00/0000 then when I receive I need to convert it to 0000/00/00. The value field is in my form as "value[]" with a coin mask 1,000.00 but I need to convert it to 1000.00. I have tried to do the conversion before and also inside the loop but 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());
    }
  • specify the form appropriately.. You don’t know what 00/00 .. The stretch 0000, by deduction is the year. But what is the position of the month and day? Example yyyy/mm/dd

  • in the form: dd/mm/yyyy but in mysql you have to write yyyy/mm/dd

  • in mysql, it normally uses the default iso8601 yyyy-mm-dd.. but depending on the version it also accepts / (bar)

  • in the form, the user inserts the date freely? is an input text field? it is safer and simpler to set a select field for day, another for month and another for year.

  • Opa brother do not know if it will suit you !!! more look if you can implement this class to your code !! Class link of a look here !

  • Daniel, sorry my error, in mysql is yyyy-mm-dd same

Show 1 more comment

2 answers

1


You could do this as follows to format the fields:

<?php

//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());

}
  • @yure_pereira , thanks for the help, perfect. RESOLVED, thanks to all for the valuable help

0

here is php good :) make a explode the original string then contact everything and forms a single string or then in java script.

<?php
    $str = 'one,two,three,four';
    // zero limit
    print_r(explode(',',$str,0));
    print "<br>";
    // positive limit
    print_r(explode(',',$str,2));
    print "<br>";
    // negative limit 
    print_r(explode(',',$str,-1));
?>  

Browser other questions tagged

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