How to format Date field in Ionic Form / Angularjs

Asked

Viewed 752 times

2

I have a date field on a form that looks like this:

<label class="item item-input item-floating-label">
            <span class="input-label">Validade da Oferta</span>
            <input type="date" ng-model="product.cadastra_oferta_validade_oferta" placeholder="Validade da Oferta" />               

        </label>

But it does not store in my PHP, because the date format in the database is different. Any hint how to solve this problem? Follow my PHP:

<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

    //formulário

    $data = file_get_contents("php://input");
    $objData = json_decode($data);

    // TRANSFORMA OS DADOS

    $cod_categoria_com = $_GET['cod_categoria_com'];
    $titulo_promocao = $_GET['titulo_promocao'];
    $descricao = $_GET['descricao'];
    $igredientes = $_GET['igredientes'];
    $foto = $_GET['foto'];
    $valor_sem_desconto = $_GET['valor_sem_desconto'];
    $valor_com_desconto = $_GET['valor_com_desconto'];
    $validade_oferta = $_GET['validade_oferta'];
    $estoque = $_GET['estoque'];
    $cod_fornecedor = $_GET['cod_fornecedor'];
    $cod_categoria = $_GET['cod_categoria'];
    $imagem = $_GET['imagem'];
    $desconto = $_GET['desconto'];

     // INSERE OS DADOS
    $db = new PDO("mysql:host=HOSTr;dbname=BANCO", "USER", "SENHA");


    if($db){

        $sql = "INSERT INTO cadastra_oferta (cod_fornecedor, cod_categoria_com, titulo_promocao, descricao, igredientes, foto, valor_sem_desconto, valor_com_desconto, desconto, validade_oferta, qtd_estoque) VALUES ('$cod_fornecedor', '$cod_categoria', '$titulo_promocao', '$descricao', '$igredientes', '$imagem', '$valor_sem_desconto', '$valor_com_desconto', '$desconto', '$validade_oferta', '$estoque')";


        $query = $db->prepare($sql);        

        $query ->execute();        

        echo json_encode(array('message'=> ' Os dados foram inseridos com sucesso. Obrigado e bem vindo!' ));
    }else{
        echo json_decode(array('message'=> ' Não foi possivel iserir os dados! Tente novamente mais tarde!' ));
    };



?>
  • 1

    what format the input sent?

  • 1

    Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

  • Value returned from Date : 1490842800000 Thu Mar 30 2017 00:00:00 GMT-0300 (Official Time of Brazil)

3 answers

1

The bank saves the date and international format year-month-day 2017-01-12. If the form is being completed in Brazilian format 12/10/2017 you must convert the date before sending to the bank.

A simple way to do it is like this:

$data = implode("-",array_reverse(explode("/",$data)));

Another thing, you’re sending the customer data directly to the bank, that’s a little problematic. Can generate several inconsistencies and more errors if no mandatory field is filled in or with a different data type.

  • Value returned from Date : 1490842800000 Thu Mar 30 2017 00:00:00 GMT-0300 (Official Time of Brazil)

  • Guys, thank you so much! :)

1

Friend, you can create a Directive that format to the pattern you need. Follow another post as reference.

Date form

  • 1

    Please collaborate with the quality of community content by better elaborating your response. Very short answers will hardly be clear enough to add anything to the discussion. If your response is based on some function or feature of the language/tool, link to the official documentation or reliable material that supports your response. Searching for similar questions in the community can also be interesting to indicate other approaches to the problem.

1


  • Hi @Romulus, registered the date as: 1969-12-31 00:00:00

  • Curious. Are you using any method or trying to pass the object directly through the request to PHP? See here how the object Date of the input behaves in the Angular: https://plnkr.co/edit/osXfSNdz5PK1g0O6a6bN

Browser other questions tagged

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