download2014711 2017-03-15 12: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-15 17: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!

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

报告相同问题?

悬赏问题

  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部