I have a form with a jQuery datepicker. I have set the date format to the European way: dd-mm-yy in order to make it look nice for the user. However this date value eventually needs to be formatted back in PHP to yy-mm-dd so that in can be stored in my mysql DATE field. Here is my code:
Jquery:
$("document").ready(function(){
$(function() {
$( ".datepicker" ).datepicker({
monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
dateFormat:'dd-mm-yy',
});
});
You have three options (in some vague order of preference):
Use MySQL’s
STR_TO_DATE()function to convert the string:Convert the string received from jQuery into a PHP
DateTimeobject:and then either:
obtain a suitable formatted string:
obtain the UNIX timestamp:
which is then passed directly to MySQL’s
FROM_UNIXTIME()function:Manually manipulate the string into a valid literal: