0

I am storing when events happened on my site in the database using the time() function and echoing them out with the date() function.

My only problem is that it echoes it out as UTC but what if my users are elsewhere in the world? How would I detect their timezone and pass it out to the date() function?

Is this even possible?

Thanks!

user115422
  • 4,662
  • 10
  • 26
  • 38
  • If you use `mysql` - you could use `timestamp` column and mysql will do the left for you. Also - for formatting and arithmetic purposes you could use `DateTime` php class instead of plain old `date()` – zerkms Aug 22 '12 at 03:00
  • @zerkms That is an inefficient way of doing this. – FThompson Aug 22 '12 at 03:01
  • im confused... i dont know how to convert timestamp into readable time and what is DateTime in php? also I would prefer not to change my database too much as it is already being closed beta tested. – user115422 Aug 22 '12 at 03:02
  • 1
    @Vulcan: uhm, I'm sorry? Any explanations? – zerkms Aug 22 '12 at 03:02
  • Refer to: http://stackoverflow.com/questions/13/determining-a-web-users-time-zone – FThompson Aug 22 '12 at 03:03
  • 1
    @Vulcan, how is it a duplicate? I didnt find that nor did I create that – user115422 Aug 22 '12 at 03:03
  • @Vulcan, if possible, I dont want to use JS because I want to include it in a file known as "settings.php" and i want to be able to edit headers afterward – user115422 Aug 22 '12 at 03:04
  • My bad, not so much a duplicate as a referral. You don't need to use JS. Notice that the accepted answer has 46 upvotes for 38 downvotes. Check out the other answers. – FThompson Aug 22 '12 at 03:05
  • @Vulcan: would you explain what's wrong with my comment? – zerkms Aug 22 '12 at 03:06
  • @Vulcan can you help me a bit with understanding how I would use "JD Isaacks" answer as it seems the most practical, so would I just set it as a variable and then subtract that from time() and then pass it into a date() function? – user115422 Aug 22 '12 at 03:08
  • @MuqMan "JD Isaacks"'s answer uses JS. zerkms, At a second look, how would your proposed solution even work? MySQL does not know the user's time offset. – FThompson Aug 22 '12 at 03:12
  • @Vulcan so is it possible with PHP? Because I need the headers editable after the timezone detect – user115422 Aug 22 '12 at 03:14
  • It's easy to manipulate the result of `date`, but the point is, you need to use _users'_ timezone, which is beyond PHP's range. I think a practical solution would be JS. Send your UTC time to client, and use JS on clients' side to convert them to whatever timezone. – Passerby Aug 22 '12 at 03:17
  • 1
    @MuqMan PHP is server-side, so no. You either will need to use a client-side language such as JS, or ask your users to choose their timezone and then store it in a cookie. – FThompson Aug 22 '12 at 03:18
  • could this be done when the user logs in? and then set as a session variable without disrupting the header? @Passerby, can I please see the "BEST" javascript? Please put it in an answer and I will accept it. – user115422 Aug 22 '12 at 03:19
  • @Vulcan ok as I said before, can this be done when the user logs in and then stored as a session variable without disrupting the header? – user115422 Aug 22 '12 at 03:20
  • @MuqMan Look at the question I linked earlier in these comments; it contains JS answers. The accepted answer provides low-quality JS, so I recommend looking at others. – FThompson Aug 22 '12 at 03:22
  • @Vulcan but how would I get the JS to interact with the PHP as I store user session info in a table? it wouldnt work exactly would it? – user115422 Aug 22 '12 at 03:25
  • @MuqMan I don't understand what exactly you're asking, but you shouldn't need to store timezone with user session info. – FThompson Aug 22 '12 at 03:28
  • @Vulcan: my comment was addressed to the second question part. When you already know user's timezone and need to perform some TZ arithmetics – zerkms Aug 22 '12 at 09:04

1 Answers1

1

OK, if you prefer the session way, place a hidden field in user login form, and use JS to give it a value. Simple illustration:

<form>
<input type="text" name="username">
<input type="password" name="password">
<input type="hidden" name="offset" id="offset">
</form>
<script language="JavaScript">
document.getElementById("offset").value=(new Date).getTimezoneOffset();
</script>

And on PHP side, manipulate the result of date like this:

$_SESSION["offset"]=intval($_GET["offset"]);
/*works here...*/
$date=date("Y-m-d",time()-($_SESSION["offset"]*60));

And if you want to do it all clients way, also use Date().getTimezoneOffset() to manipulate the result.

MDN Document

Passerby
  • 9,715
  • 2
  • 33
  • 50