Although the value displayed by the browser is based on the local configuration of the browser, in fact the data that will be sent will ALWAYS be coded as AAAA-MM-DD
:
It should be noted that the date format shown differs from the value
itself: the date format displayed will be chosen based on the user's browser's regional settings, although the value
date is always formatted as aaaa-mm-dd
.
It's just the one MySQL requires for its fields DATETIME
:
MySQL recognizes values DATE
in the following formats:
MySQL recognizes values DATETIME
and TIMESTAMP
in the following formats:
Entering a value AAAA-MM-DD
in a field of type DATETIME
will result in a conversion of type DATE
to DATETIME
, setting the hour, minutes and seconds to 0 ( AAAA-MM-DD 00:00:00
).
You can concatenate both values in PHP to generate a string of the last type:
<?php
$datetime = $_POST['fecha'] . ' ' . $_POST['hora'];
Although you should keep in mind that this date will be in local time slot, so you will have to take it into account by sending the user's strip to store the dates in the database in UTC whenever possible.
Here is a practical example showing that the format sent will be correct:
function comprueba(form) {
/* Calculamos la fecha en formato UTC. La sustitución
de T por " " no es estrictamente necesaria */
form.datetime.value = new Date(
form.fecha.value + ' ' + form.hora.value
).toISOString().substr(0,19).replace('T', ' ');
console.log('Fecha: ', form.fecha.value);
console.log('Hora: ', form.hora.value);
console.log('Datetime en UTC: ', form.datetime.value);
return false;
}
window.onload = function() {
/* Ponemos la fecha y hora actual */
var fecha = new Date();
document
.querySelector('input[type="date"]')
.value = fecha.toISOString().substr(0,10);
document
.querySelector('input[type="time"]')
.value = fecha.toTimeString().substr(0, 5);
}
<form onsubmit="return comprueba(this)">
<input type="date" name="fecha" />
<input type="time" name="hora" />
<input type="hidden" name="datetime" />
<input type="submit" />
</form>