Data Manipulation in PHP

Asked

Viewed 53 times

0

Good afternoon to all.

I would appreciate an idea to solve my problem:

My start and end date fields are being sent to the BD using this preg_replace:

   public function getDatainicio()
   {  

   return $this->datainicio;

    }
   public function setDatainicio($datainicio)
 {   
 $datainicio= preg_replace("/\D+/", "", $datainicio); 
$this->datainicio = $datainicio;
 }

 public function getDatatermino()
 {     
  return $this->datatermino;
  }
 public function setDatatermino($datatermino)
 {   
$datatermino= preg_replace("/\D+/", "", $datatermino); 
$this->datatermino = $datatermino;
  }

I am able to enter without prolemas. But when I need to create the change form I am returning the data from BD I receive this way:

["datahome"]=> string(8) "20180921" ["datatermino"]=> string(8) "20180923" I need to convert to Brazilian default and display in html and then convert again if the user wants to change the date and send to BD again with the date it accepts.

I’ve tried to change directly in HTML using javascript but could not result think it is because it would have to receive in BD 2018-09-21 format and not 20180921.

Why this is happening and what is the best method to solve?

This is my html:

  <?php ini_set('display_errors', 1);?>
  <?php error_reporting(E_ALL);?>
 <?php require_once 'conecta.php' ?>
 <?php require_once 'cabecalho.php' ?>
 <?php require_once 'banco-curso.php' ?>
 <?php require_once 'banco-fornecedor.php' ?>
  <?php require_once 'curso.php' ?>
 <?php require_once 'fornecedor.php' ?>

 <?php
 $curso = new Curso();
 $curso->setFornecedor(new Fornecedor());
 if (array_key_exists('id', $_GET)) {
 $id = $_GET['id'];
 $curso = buscaCurso($conexao, $id);
 }
 ?>

 <?php $fornecedores = listaFornecedores($conexao); ?>

 <?php
 $ehAlteracao = false;
 $action = "adiciona-curso.php";
 if (array_key_exists("id", $_GET)) {
 $id = $_GET['id'];
 $curso = buscaCurso($conexao, $id);
 $ehAlteracao = true;
 $action = "altera-curso.php";
  }
  ?>
    <div class="container">
     <center><h1 class="h3 mb-3 font-weight-normal"> <?=$ehAlteracao ? 
   "Alterar" :  "Cadastrar " ?>
     o Curso</h1><form action="<?=$action ?>"  method="post" ></center>
      <div class="form-group">

  <input type="hidden" name="id" value="<?=$curso->getId() ?>" />

  <div class="form-group col-md-12">
  <label for="inputEmail4">Nome do Curso</label>
  <input type="text" class="form-control" id="nome" name="nome" 
   placeholder="nome do curso" required 
  value="<?=$curso->getNome() ?>">
   </div>

 <div class="form-group col-md-4">
  <label for="inputDataInicio">Data Inicio</label>
  <input type="date" class="form-control" id="datainicio" name="datainicio"
  value="<?=$curso->getDatainicio() ?>"   required>

</div>  

   <div class="form-group col-md-4">
  <label for="inputDataTermino">Data Término</label>
  <input type="date" class="form-control" id="datatermino" 
    name="datatermino" 
  value= "<?=$curso->getDatatermino()?>" required>

</div>


   <div class="form-group col-md-8">
  <label for="professor">Professor</label>
  <input type="text" class="form-control" id="professor" name="professor" 
     placeholder="professor" required 
  value="<?=$curso->getProfessor() ?>">
 </div>

  <div class="form-group col-md-8">
  <label for="local">Local do Curso</label>
  <select name="fornecedor_id" class="form-control">
  <option selected>Escolher...</option>
      <?php foreach($fornecedores as $fornecedor)  :?>
 <?php
     $essaEhAfornecedor = $curso->getfornecedor()->getId()
     == $fornecedor->getId();
     $selecao = $essaEhAfornecedor ? "selected='selected'" : "";
 ?>
    <option value="<?=$fornecedor->getId() ?>" <?=$selecao ?> >
        <?=$fornecedor->getCidade() ?>
    </option>
        <?php endforeach ?>
 </select>
 </div>
 <br>
<center><button type="submit" name="submit" id="registrar"
   class="btn btn-primary" ng-disabled="!checked">CADASTRAR</button> 
  </center>

 </div>

This is my bank in the functions alters and search

 function buscaCurso($conexao, $id) {
  $query = "select c.*, f.cidade as fornecedor_cidade from cursos c
  inner join fornecedores f on(c.fornecedor_id = f.id)
  where c.id = {$id}"; 

   $resultado = mysqli_query($conexao, $query);
   $array = mysqli_fetch_assoc($resultado);        
   $curso = new curso();
   $curso->setId( $array['id'] );
   $curso->setNome( $array['nome'] );
    $curso->setFornecedor( new Fornecedor() );
    $curso->getFornecedor()->setId( $array['fornecedor_id'] );
    $curso->getFornecedor()->setCidade( $array['fornecedor_cidade'] );
    $curso->setDatainicio( $array['datainicio'] );
    $curso->setDatatermino( $array['datatermino'] );
    $curso->setProfessor( $array['professor'] );
    var_dump($curso); exit; 

    return $curso;


        }
  • Which database are you using? Mysql, oracle, ...

  • I am using Mysql

  • The date field in the bank is date type same?

  • that even Adventistaam is date

  • have tried using the function DATE_FORMAT mysql? Exem. SELECT DATE_FORMAT(campoData, '%d/%m/%Y' FROM suaTabela) to retrieve the date in Brazilian format

No answers

Browser other questions tagged

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