0

I am coding an App which users can post and it keeps a timestamp. I generate the timestamp in PHP using date('Y-m-d H:i:s'). Then when I display the posts the App calculates the time difference in c# to display in time ago such as 1 day ago or 1 hour ago and when you view post info it shows the exact date in the database.

The problem is when I create a post it displays as 8 hours ago since the PHP database isn't in the same timezone as the phone. I was thinking about calculating the time ago in the PHP but then in the post date, it would be off.

Also I can't do it off the devices time in case the user changes their settings then they can change their date back years and post which will create a bug.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Dan
  • 1,100
  • 2
  • 13
  • 36

1 Answers1

2

Generally, the best practice is to generate timestamps on the server in UTC time, and then if necessary adjust them on the client to display in the user's local time.

You can use php's gmdate() function to get the current UTC time.

Jason
  • 86,222
  • 15
  • 131
  • 146
  • Use ToLocalTime – Jason Feb 21 '18 at 03:58
  • I tried using `gmdate('Y-m-d H:i:s')` and `Convert.ToDateTime(message.TimeStamp).ToLocalTime()` but it says 8 hours ago when I post. – Dan Feb 21 '18 at 04:02
  • try appending "Z" to the end of the time - see https://stackoverflow.com/questions/1756639/why-cant-datetime-parse-parse-utc-date. Or use a less ambiguous method of passing datetime, like time() – Jason Feb 21 '18 at 04:29