Display date format in "d-m-y" and save to a database in "y-m-d" format

1

I have to show the format D-M-Y but it must be saved in the MySQL database, Y-M-D . I am using Bootstrap for the calendar of dates and PHP to perform the maintenance on the table.

Code HTML and PHP :

<div class="form-group">
    <label class="control-label col-xs-3">Fecha Inicio :</label>
    <div id="datetimepicker1" class="col-xs-3 input-group">
        <input data-format="yyyy-MM-dd" type="text" class="form-control" id="fecIni" name="fecIni" ></input>
        <span class="input-group-addon add-on">
            <i data-time-icon="fa fa-times" data-date-icon="fa fa-calendar" ></i>
        </span>
    </div>
</div>

At the moment I modified the data-format to dd-mm-yyyy , yes, I visualize this format, but I get an error when saving and it is because of the format type entered.

However, when I remove the data-format it is also displayed in dd-mm-yyyy format, but it can not be saved.

For maintenance I use Stores Procedures and the date is declared as DATETIME .

    
asked by Silaboga 17.01.2017 в 21:50
source

2 answers

0

Use the native date functions of PHP . First pick up the value of your input:

$fecha = $_POST['fecIni'];

Then a date conversion would be done as follows:

$fechaBD = date("d-m-Y", strtotime($fecha));

At the end the variable $fechaBD is the one that would be inserted in Database .

Source of information:

  

link

    
answered by 17.01.2017 / 23:14
source
0

Well, I have a method that can work, although there may be a simpler one, I imagine that before saving your date you receive it by post

$fecha = $_POST['fecIni'];

well, then divide it and create a new variable with the date in the format Y-m-d

the code would be something like this ....

$fecha = $_POST['fecIni'];
$partes = explode('-', $fecha);
$_fecha = "{$partes[2]}-{$partes[1]}-{$partes[0]}";

and at the moment of inserting you only use the variable $ _ date and you should insert it in "Y-m-d" format

I hope you serve as a friend although I repeat, there could be an even simpler function ... regards

    
answered by 17.01.2017 в 23:01