mobix
mobix

Reputation: 515

How to insert DateTime form flutter to PHP MySql using POST

I'm currently working on flutter with mysql and php. connection to my DB and flutter works fine. and when i insert String it works perfectly. but when i use DateTime.now() it throws Error: FormatException: SyntaxError: Unexpected token b in JSON at position 0.

my code from flutter 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'
  });

and my PHP side to receiving the post of date looks like

$created = date('Y-m-d', strtotime($_POST['created']));

so how can i insert date from my flutter and receive it from php?

Upvotes: 1

Views: 1049

Answers (1)

mobix
mobix

Reputation: 515

Let me just post my solution if it helps anyone. first we need to create a column in our db as DateTime not Date. then in php we initiate it as $created = date('Y-m-d', strtotime($_POST['created']));, after this in flutter we can assign it as 'created': DateTime.now().toString(),. this worked for me.

Upvotes: 1

Related Questions