0

How can we detect user's local time? For example,

My server is located in USA and date time is 27-Mar-2014 01:10:10. (GMT0) I use my PC access to my website from Cambodia and date time is 27-Mar-2014 08:10:10. (GMT+7).

How can I get my PC datetime when I access to my website at USA?

Please help!

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
  • You cannot. You could provide a way for the user to specify his/her local time zone, and store it in the user profile, but AFAIK there is no way you can determine a client's time zone from the browser alone. – lethal-guitar Mar 27 '14 at 11:30
  • 1
    On another note, any time in the US is certainly not GMT, but has a negative offset. If your server actually reports it's local time as GMT (+/-0), then it's not configured properly. – lethal-guitar Mar 27 '14 at 11:32
  • Hi! In fact, before I insert datetime to database, I convert it to UTC or GMT0. – Richat CHHAYVANDY Mar 27 '14 at 11:34
  • Hi! Can we detect client's IP Address then detect country? – Richat CHHAYVANDY Mar 27 '14 at 11:35
  • 1
    Perhaps, this is what you are looking for.[The link](http://stackoverflow.com/questions/10659523/how-to-get-the-exact-local-time-of-client) – Vagabond Mar 27 '14 at 11:37

2 Answers2

1

This code works fine for me

  echo <<<EOE
   <script type="text/javascript">
     if (navigator.cookieEnabled)
       document.cookie = "tzo="+ (- new Date().getTimezoneOffset());
   </script>
EOE;
  if (!isset($_COOKIE['tzo'])) {
    echo <<<EOE
      <script type="text/javascript">
        if (navigator.cookieEnabled) document.reload();
        else alert("Cookies must be enabled!");
      </script>
EOE;
    die();
  }
  $ts = new DateTime('now', new DateTimeZone('GMT'));
  $ts->add(DateInterval::createFromDateString($_COOKIE['tzo'].' minutes'));
Lauber Bernhard
  • 175
  • 1
  • 1
  • 6
0

You would have to send it to the server using javascript(which runs on the client and can access the system time)

Chrisi
  • 371
  • 3
  • 15