J.L J.L - 1 month ago 15
MySQL Question

How to store time picker into database [CLOSE]

Php code:

$odr_time = (isset($_POST['order_time'])) ? $_POST['order_time'] : "";


$Q_insert = "INSERT INTO dsp_order_item SET order_time = '".$ord_time."';


Android:

etTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(),
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

etTime.setText(String.format("%02d:%02d", hourOfDay, minute));
String order_time = etTime.getText().toString();
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
});


Previously, the DATE [SOLVED] and the TIME become empty where the field does not has any value. Is it because I set the data type 'VARCHAR' so it does not store it?

Any suggestion?

Answer

you want to insert date to database, you can use this code.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());

In database insert the string 'date'

The date format in sqlite should be of following format:

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD 

For more details, have a look: http://www.sqlite.org/lang_datefunc.html

Comments