In a web application under jQuery
, I'm using datepicker
of jQuery UI
.
At datepicker
I apply a regional setting so that the date is delivered in dd/mm/yyyy
format.
The user selects a date in the datepicker
, which sent by Ajax
to php
to be recorded in MySQL
and then, through another Ajax
recovered the date that was recorded .
For example, the user selects 18/11/2016
in datepicker
, but in MySQL
when recording this date, it remains as 000-00-00
; it's like he did not understand the date and recorded a 0
.
To solve the above, in the php
that receives the date, I did the following:
$fechaz = $_POST["fecha"];
$partes = array();
$partes = explode("/",$fechaz);
$arreglo = array($partes[2], $partes[1], $partes[0]);
$nueva_fecha = implode("-", $arreglo);
$con = conectar();
$sql1 = "INSERT INTO fechas (fecha) VALUES ('$nueva_fecha')";
$q1 = mysqli_query( $con, $sql1
In this way, MySQL
is now recorded in 2016-11-18
.
When you retrieve this date from MySQL
in the next Ajax
, and display it in another datepicker
, a date appears that does not correspond 28/04/2022
. I tried to do the same in the php
that gets the date, using explode()
e implode()
, but it does not work with the recovered date.
In this regard, the questions are:
Is there a way, through statements or functions of jQuery
that allow, in the case of recording the date, transform the format of datepicker
dd/mm/yyyy
to the format required by MySQL yyyy-mm-dd
, of so that in php
you do not have to use explode()
e implode()
?
And related to the same thing, is there a sentence or function that allows me to transform the format of the date that comes from MySQL yyyy-mm-dd
and take it to dd/mm/yyyy
, in order to display it correctly in datepicker
?
I have taken the suggestion of @aldanux and it worked for me to save the date in MySQL
, but to present in the datepicker
the recovered date, it does not work for me:
The invocation to record and retrieve the date is:
var fecha1 = $("#datepicker").val();
$.ajax({
url: "grabar_fecha.php",
data: "fecha=" + fecha1,
type: "POST",
dataType: "json",
success: function (respuesta) {
console.log("GRABADO");
}
});
$.ajax({
url: "recupera_fecha.php",
data: "saludo=" + "buscar",
type: "POST",
dataType: "json",
success: function (res) {
$("#datepicker").datepicker("setDate", res.fecha);
}
});
The% co_of% that you recorded was as:
require_once "funciones/conexiones.php";
$fechaz = $_POST["fecha"];
$f = str_replace('/', '-', $fechaz);
$fx = date('Y-m-d', strtotime($f));
$con = conectar();
$sql1 = "INSERT INTO fechas (fecha) VALUES ('$fx')";
$q1 = mysqli_query( $con, $sql1) or die("Problemas al ejecutar la consulta");
This is recording the date correctly, in my example php
The 2016-11-18
that recovers the date was as:
require_once "funciones/conexiones.php";
$saludo = $_POST['saludo'];
$con = conectar();
$sql = "SELECT max(id) as id, fecha FROM fechas";
$q = mysqli_query($con, $sql);
$info = array();
while ($datos = mysqli_fetch_array($q)) {
$ident=$datos["id"];
$fec=$datos["fecha"];
};
$fx = date('Y-m-d', strtotime($fec));
$info['id'] = $ident;
$info['fecha'] = $fx;
echo json_encode($info);
This is not working well, because the date you recover is php
.
What is missing?