0

I have a MySQL database that records the time when a given event occurs. There's a web app that show's these times when you load the page BUT its in +0 TimeZone. I want this to show what ever timezone that the user is in.

echo(date('l F jS Y h:i:s A', ($row["date"]/1000)- 3600*6));

The above code shows that if I want the timezone to be central I have to *6. But this would just be the new timezone then and doesn't change based on users location.

Also a added bonus would to be able to display at the top of the page somewhere the users IP address, City and State.

EDIT: Trying to get the UTC timezone # into a $variable. Example being Central Time would = -6.

echo(date('l F jS Y h:i:s A', ($row["date"]/1000)-(3600*(-1*$variable))));
Pie
  • 856
  • 4
  • 12
  • 33
  • [See second answer from top](http://stackoverflow.com/questions/4746249/get-user-timezone) – Richard May 07 '15 at 16:55
  • You can do it accurately with JavaScript, is that an option? – Halcyon May 07 '15 at 16:56
  • I tried the javascript answer on that page, but I found out that the PHP will always load first before javascript. Server (PHP) vs Client (Javascript) code rules. I then tried the first answer (most upvoted one) but I'm not able to print out anything. What I want to try and have print out is the UTC # related to the time zone. Example being "Eastern Time (-5)", so print -5. – Pie May 08 '15 at 02:14
  • So the line of code that converts the data would look something like this echo(date('l F jS Y h:i:s A', ($row["date"]/1000)-(3600*(-1*$variable)))); – Pie May 08 '15 at 02:21

1 Answers1

0

If you want to have the correct time zone set on page load you are going to need to store the users time zone as a preference in your application. Unfortunately PHP (or any backend language) for that matter does not have access to such browser information (unless you additionally pass it to the server-side script).

The other option is to pass the UTC date or server timezone date, get the user's time zone and convert your date to their local time using Javascript.

You can get the UTC time zone offset with

var d = new Date()
d.getTimezoneOffset();
Colin Schoen
  • 2,526
  • 1
  • 17
  • 26
  • Thanks but this was one of the comments I replied to above. I don't think I can make javascript work and if so I don't know how. I have something already to grab the users ip that is stored in $ip. Just need to get it to show the ip number. – Pie May 08 '15 at 02:34