search

Home  >  Q&A  >  body text

How to insert DateTime form flutter into PHP MySql using POST

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粉282627613P粉282627613357 days ago425

reply all(1)I'll reply

  • P粉226642568

    P粉2266425682024-01-06 00:31:28

    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.

    reply
    0
  • Cancelreply