I am currently developing flutter using mysql and php. The connection to my database and flutter works fine. It works fine when I insert string. But when I use DateTime.now()
it throws Error: FormatException: SyntaxError: Unexpected token b in JSON at location 0
.
My flutter code is
DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss"); String date = dateFormat.format(DateTime.now()); var url = 'http://192.168.1.8/konkolata-dashboard/driver.php'; var response = await http.post(Uri.parse(url), body: { 'name' : fullName.text, 'assigned' : 'false', 'assignedTo': 'no', 'created': date, 'available': 'true' });
My PHP side receiving the date of the post looks like
$created = date('Y-m-d', strtotime($_POST['created']));
So how can I insert date from flutter and receive it from php?
If it helps anyone, please let me post my solution. First, we need to create a column in the database as DateTime instead of Date. Then in php we initialize it as
$created = date('Y-m-d', strtotime($_POST['created']));
, and then in flutter we can specify it as"Create": DateTime.now().toString(),
. This works for me.