Problem with date in php and phpmyadmin

0

I have a problem to store the time in my database, I have a form from which a series of data is sent but for some reason the date is not inserted or only remain in zeros.

            <div class="form-group">
              <label>Fecha y hora de Inicio:</label>
              <div class="input-group">
                <div class="input-group-addon">
                  <i class="fa fa-clock-o"></i>
                </div>
                <input type="date" id="fecha1" name="fecha1">
              </div>
            </div>

            <div class="form-group">
              <label>Fecha y hora de Término:</label>
              <div class="input-group">
                <div class="input-group-addon">
                  <i class="fa fa-clock-o"></i>
                </div>
                <input type="date" id="fecha2" name="fecha2">
              </div>
            </div>        

There are the inputs that I have in the form since everything else works correctly.

function add () {

global $db, $_POST, $_FILES;

$uniqid = uniqid();
$filename = $uniqid . basename($_FILES["cupon_imagen"]["name"]);
$target_file = $_SERVER['DOCUMENT_ROOT'] .'/admin-kupom/content/cupones/'. $filename;
$filename = 'http://www.kupomcity.com/admin-kupom/content/cupones/' . $filename;

$destacado = 0;
if ($_POST['destacado'] == 1) {
  $destacado = 1;
}

if (move_uploaded_file($_FILES["cupon_imagen"]["tmp_name"], $target_file))
{
  $sql = "INSERT INTO cupones (
    id_comercio,
    destacado,
    cantidad,
    image,
    titulo,
    texto,
    duracion,
    created_by,
    created_at,
    fecha_termino
  ) VALUES (
    :id_comercio,
    :destacado,
    :cantidad,
    :image,
    :titulo,
    :texto,
    :duracion,
    :id_usuario,
    :data1,
    :data2
  )";

  $stmt = $db->prepare($sql);
  $stmt->bindParam(':id_comercio', $_POST['id_comercio'], PDO::PARAM_INT);
  $stmt->bindParam(':destacado', $destacado, PDO::PARAM_STR);
  $stmt->bindParam(':data1', $_POST['fecha1'], PDO::PARAM_STR);
  $stmt->bindParam(':data2', $_POST['fecha2'], PDO::PARAM_STR);
  $stmt->bindParam(':cantidad', $_POST['cantidad'], PDO::PARAM_STR);
  $stmt->bindParam(':image', $filename, PDO::PARAM_STR);
  $stmt->bindParam(':titulo', $_POST['titulo'], PDO::PARAM_STR);
  $stmt->bindParam(':texto', $_POST['texto'], PDO::PARAM_STR);
  $stmt->bindParam(':duracion', $_POST['duracion'], PDO::PARAM_STR);
  $stmt->bindParam(':id_usuario', $_SESSION['user']['id'], PDO::PARAM_INT);
  $stmt->execute();
  return true;
} else {
  return false;
}

}

Here is my function to insert the information into the database, I do not know what is wrong or what I need to insert the dates in the database. The fields are of type date in my database table.

    
asked by Nelson Jara Torres 19.07.2017 в 23:04
source

2 answers

0

Very good estimate. If you want to put for the form, you just have to add this for PHP, what it does for the form, as I see it is datetime:

<input type="text" name="fecha" value="<?php date_default_timezone_set('America/Santiago'); echo date('Y-m-d H:i:s');?>"><br>

date_default_timezone_set('America/Santiago') can be changed to the corresponding country, say 'Europe / Berlin' of germany, capital: berlin.

I leave this link so you can understand List of supported time zones

I hope it helped you! Greetings

    
answered by 20.07.2017 / 07:36
source
0

Try this:

$nuevafecha = date( "Y-m-d H:i:s", strtotime(str_replace("(Hora de verano romance)", "", $fecha)));

The time taken by the input date is in this format:

Sun Jul 02 2017 02:00:00 GMT + 0200 (Summer time romance)

With the function above, we convert it to the Date format for the database.

    
answered by 19.07.2017 в 23:09