user115422
user115422

Reputation: 4710

PHP offset date() result based on user's timezone?

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!

Upvotes: 0

Views: 207

Answers (1)

Passerby
Passerby

Reputation: 10070

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

Upvotes: 1

Related Questions