1+ <?php
2+
3+ /*
4+ * (c) Kevin Orriss <[email protected] > 5+ *
6+ * For the full copyright and license information, please view the LICENSE
7+ * file that was distributed with this source code.
8+ */
9+
10+ namespace KevinOrriss \ClientTimezone ;
11+
12+ use Session ;
13+
14+ /**
15+ * A simple API extension for getting a users timezone via JavaScript
16+ */
17+ class ClientTimezone
18+ {
19+ /**
20+ * The session key where the timezone offset is stored
21+ *
22+ * @var string
23+ */
24+ const CLIENT_TIMEZONE_SESSION = "clienttimezone " ;
25+
26+ /**
27+ * The URL entered for the Ajax post in javascript.blade.php
28+ *
29+ * @var string
30+ */
31+ const CLIENT_TIMEZONE_POST = "clienttimezone " ;
32+
33+
34+ /**
35+ * Returns the session key where the timezone offset is stored.
36+ * This key can be overriden by adding CLIENT_TIMEZONE_SESSION
37+ * to the .env file
38+ *
39+ * @return string
40+ */
41+ protected static function getSessionKey ()
42+ {
43+ return env ('CLIENT_TIMEZONE_SESSION ' , self ::CLIENT_TIMEZONE_SESSION );
44+ }
45+
46+ /**
47+ * Returns the URL entered for the Ajax post in javascript.blade.php
48+ * This URL can be overriden by adding CLIENT_TIMEZONE_POST
49+ * to the .env file
50+ *
51+ * @return string
52+ */
53+ public static function getPostUrl ()
54+ {
55+ return env ('CLIENT_TIMEZONE_POST ' , self ::CLIENT_TIMEZONE_POST );
56+ }
57+
58+ /**
59+ * Returns the client timezone as the number of minutes offset from the current UTC time.
60+ * If the client timezone is not set in the session, NULL is returned
61+ *
62+ * @return integer|NULL
63+ */
64+ public static function getOffset ()
65+ {
66+ if (Session::has (static ::getSessionKey ()))
67+ {
68+ return intval (Session::get (static ::getSessionKey ()));
69+ }
70+ return NULL ;
71+ }
72+
73+ /**
74+ * Sets the number of minutes offset from the current UTC time in the session
75+ *
76+ * @param integer $offset
77+ */
78+ public static function setOffset ($ offset )
79+ {
80+ Session::put (static ::getSessionKey (), intval ($ offset ));
81+ }
82+
83+ /**
84+ * Removes the timezone offset from the session
85+ */
86+ public static function forget ()
87+ {
88+ Session::forget (static ::getSessionKey ());
89+ }
90+ }
0 commit comments