download2014711 2017-03-15 20:49
浏览 60
已采纳

PHP / MySQL和iOS非常奇怪的日期行为

I'm porting my Android app to iOS and I'm at the chat stage.

Each message has its own date, and my server API (in PHP) has a method that fetches all messages since a date (so I can send a request with my lastMessageDate and get any new messages that are not on the device whenever needed)

My database is MySQL and the server's timezone is GMT+2. The DB saves all dates as GMT+2, and PHP sends them as string back to the client.

In Android, and in my REST client for testing, calling the following request gives the "No new messages" message, as it should: works as expected

In iOS, since the OS translates all Dates to UTC, before sending the date, I turn in into a GMT+2 string. I've been testing and debugging this date string, and it is indeed in GMT+2, as my server expects it to be. Printing with the debugger the POST params before sending the request, shows that the time is indeed in GMT+2:

Params: ["since": "2017-03-15 21:16:04", "chat_id": "86", "username": "testuser"]

However, sending the exact same request, as in my REST client, returns chat messages that have been posted the past 2 hours, like if the iOS app is sending the UTC date instead of my GMT+2 string.

The response in the iOS emulator is the following:

Received data is: 
{
  "message" : "Messages in this chat:",
  "messages" : [
    {
      "message" : "This is a message",
      "chat_id" : "86",
      "who" : "1",
      "date" : "2017-03-15 21:16:04"
    }
  ],
  "error" : false
}

I've been trying to debug this for the past 4 hours with no luck. I don't know if it's the server's fault or iOS' fault, so if anyone can point me to the right direction, it would be very much appreciated. Thank you

  • 写回答

1条回答 默认 最新

  • duannengling4705 2017-03-16 01:47
    关注

    Turns out I need to encode the request with Alamofire.URLEncoding.httpBody instead of Alamofire.JSONEncoding.default in order for PHP to be able to check for parameters using isset($_POST["since"]). I didn't face this problem before, in the other 40 API calls that my app encodes with Alamofire.JSONEncoding.default.

    If anyone can explain when it's best to encode using URLEncoding and when JSONEncoding, let me know.

    Thanks to everyone who tried to help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?