0

out of all issues I would not know where to start with this and it sounds very complex if not impossible but no point giving up without asking this awesome site ;)

Anyway, What I am trying to do is detect when something was posted depending on what country there in so for example:

If in Australia,Brisbane it's 21 August 2014 12:17PM and in america,New York say for example: it's 20 August 2014, some time it will exactly say when it was posted depending on where you live. so it will say it was posted on 21 August since that's the time it was posted and if your in America it will say that time. (I want it to be around the world comfortable)

Would I be just making the default timezone Australia worried it might be a bit annoying as not everyone lives here

Thanks!

Edit:

Find some code that seems to function well and is this good enough?

$timezone = "UTC";
date_default_timezone_set($timezone);

$utc = "Aug 11 2014 02:42:38 AM";

$timezone = "Australia/Perth";
date_default_timezone_set($timezone);

$offset = date('Z', strtotime($utc));
print "LOCAL: " . date('r', strtotime($utc) + $offset) . "\n";
TheEvilCoder
  • 31
  • 1
  • 8
  • 1
    Have the application store all times in UTC. Then convert to the user's local timezone when displaying. There are PHP functions for this. – Barmar Aug 21 '14 at 02:22
  • but displaying dates as per country would lead to confusion to a general user as well. SO stick to UTC as pointed out by @Barmar – rahulmishra Aug 21 '14 at 02:27
  • @rahulmr I think most people prefer times to be displayed in their own timezone. At least in the US, few people know their offset from UTC. – Barmar Aug 21 '14 at 02:29
  • 1
    If so , here is your answer http://stackoverflow.com/questions/16525617/how-to-detect-users-timezone – rahulmishra Aug 21 '14 at 02:31
  • thanks sorry if this is duplicate but didnt know where to start of even searching this up thanks I check it out and see how i go – TheEvilCoder Aug 21 '14 at 02:32
  • @ rahulmr doesent facebook use timezone detecting :/ – TheEvilCoder Aug 21 '14 at 02:33
  • @TheEvilCoder - Sort of, but not really. See http://stackoverflow.com/a/22600557/634824 – Matt Johnson-Pint Aug 21 '14 at 17:22

2 Answers2

0

Displaying time with timezones is easy in your case.

I assume when you store a post you know the date time + a time zone let say it's utc

Then when a user open the post For his ip or some other method you know his time zone too Then displaying date is as follows 1- set ttime zone with php datetime class

From php example format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
Ali
  • 151
  • 11
  • yes that's exactly what I need :) I am posting the default time in Australia and using of sets to get the actual date for the current user so in this case I can use the user ip and thanks :D not so complex after all.. – TheEvilCoder Aug 21 '14 at 03:12
0

One common solution to this problem is to store everything in UTC. When showing the values, pass the UTC values to the browser (in an API response or a hidden HTML field), then use JavaScript to load the UTC value into a Date object.

Since the JavaScript Date object is running in the user's browser, it can convert from UTC to the user's local time zone without you ever having to know what that time zone is.

The only downside is that it can be inaccurate in certain cases, as described here. Still, it is usually a good approach.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575