0
$offset = "<script>document.write(new Date().getTimezoneOffset().toString());</script>";
Session::set("offset", $offset);
$offset=Session::get("offset");

echo intval($offset);

I am trying to convert timezone to integer. intval output is always 0.

Elie Faës
  • 3,215
  • 1
  • 25
  • 41
AlexZvl
  • 2,092
  • 4
  • 18
  • 32
  • 1
    Err, do you put your code inside quotes like that? – frz3993 Mar 13 '16 at 15:56
  • I am sorry, this is my first question posted here @frz3993 – AlexZvl Mar 13 '16 at 16:01
  • Ok, what your code implies is that you are trying to get the integer representation of a string containing alphanumericals (`" – frz3993 Mar 13 '16 at 16:10
  • You are trying to get a timezone by javascript in PHP? That's not how you should be doing it. – Thijmen Mar 13 '16 at 16:21
  • Can you tell me how I should get time offset ? – AlexZvl Mar 13 '16 at 18:26
  • @frz3993 If I echo $offset it would show me -120 which is my time offset. So , I am trying to get integer representation of -120 I want the value to be integer in order to make some operations. – AlexZvl Mar 13 '16 at 18:58

2 Answers2

0

Try this

Session::set("time", time());

Then you can get your offset in next request:

$time=Session::get("time");
echo $time;

If you need to use TimeZone then you can use Carbon instance since you're using Laravel.

$time = Carbon::now();
$timeZone = $time->timezone;
Session::put('timezone', $timeZone);

and then retrieve the timezone from session....

Can Celik
  • 2,050
  • 21
  • 30
  • Thanks for the response. Carbon::now() gets the time (Europe/London) timezone which is UTC+0 I need to get the timezone depending on the client. So I would probably need to get it with javascript. – AlexZvl Mar 13 '16 at 18:55
  • Carbon::now() shows the server timezone right? It doesn't know anything about the end-user's timezone. I would need client timezone. – AlexZvl Mar 13 '16 at 19:42
  • oh that's right. Then you need to implement something like the answer here http://stackoverflow.com/a/5607444/2951316. – Can Celik Mar 13 '16 at 20:57
0
$offset = "<script>document.write(new Date().getTimezoneOffset().toString());</script>";
Session::set("offset", $offset);

This means=

Session::set("offset", "<script>document.write(new Date().getTimezoneOffset().toString());</script>");

So your offset value is just a string "< script>document.write(new Date().getTimezoneOffset().toString());< /script>"

Your question should be how to get user timezone which is duplicate.

Determine a User's Timezone

get user timezone

Community
  • 1
  • 1
Can Celik
  • 2,050
  • 21
  • 30