Date manipulation in PHP

0

Good afternoon everyone.

I would appreciate an idea to solve my problem:

My datainicio and datatermino fields are being sent to DB 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 insert without problems. But when I need to create the change form I'm returning the DB data I get this way:

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

I have tried to change directly in the HTML using javascript but I did not get the result I think it is because it would have to receive in the form of BD 2018-09-21 and not 20180921.

Why is this 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 seat in the change and search functions

 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;


        }
    
asked by anonymous 03.09.2018 / 21:26

0 answers