How to insert DateTime form flutter into PHP MySql using POST
P粉282627613
P粉282627613 2024-01-05 17:44:52
0
1
406

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?

P粉282627613
P粉282627613

reply all(1)
P粉226642568

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template