JonYork
JonYork

Reputation: 1243

user insert date in form

I have a form where the user needs to insert a date. This date is then used by the program to determine an expiry date. My question is, how is the best method for the user to insert the date in the form, for it to be stored in a mysql date cell.

Thanks!

Upvotes: 0

Views: 5004

Answers (2)

fedeisas
fedeisas

Reputation: 2023

I agree with @treeface. Use jQUery's DatePicker, but use the date format localized for your country users. It happened to me, that around the world people uses diferent time formats. Pay attention to that. You can store that time in your database using MySQL DATE (YYYY-MM-DD), DATETIME (YYYY-MM-DD HH:MM:SS) or UNIX TIMESTAMP. Comparing dates it's easy, there are lot of scripts to read about that. Use your controleer to send the data to your model, compare, and return TRUE/FALSE depending on the result. With that boolean, you can show the results to your user. Also, you can accomplish that using a jQuery. Send the selected date to the server, compare, and return a JSON response with and OK/NOTOK status.

For example:


<head>
....
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<link rel="stylesheet" href="path/to/jquery-ui-1.8.4.css" type="text/css" media="screen" />
<script type="text/javascript" src="path/to/jquery-ui-1.8.4.min.js"></script>
....
</head>

<script type="text/javascript">
$("input#datepicker").datepicker({
    showOn: 'both',
    buttonImageOnly: true,
    buttonImage: 'images/calendar-trans.png',
    firstDay: 1,
    dateFormat: "dd/mm/yy", // Select a time format that suits you
    changeMonth: true,
    changeYear: true,
    onSelect: function(dateText, inst) { 
        $.post(url+"/calendar/check_date/",
        {
            date_selected: $("input#datepicker").val();

        },function(data){
            if(data.status != 'OK'){
                alert('Your date is wrong. Try again!');
            }
        });
    }
});
</script>

Upvotes: 1

treeface
treeface

Reputation: 13351

Use something like this for the front end:

http://jqueryui.com/demos/datepicker/

If you don't want to manipulate the date string in PHP, set the datepicker to work in YYYY-MM-DD format (which is used in the MySQL DATE type). Then either send your information off with Javascript or as a part of a form.

Upvotes: 2

Related Questions