Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

Users' time - Not Server time

Users' time - Not Server time

Hi,

MetaMod rocks!

Quick question - I want to show modules according to a Users' local time, NOT the server's time. Does the following code do this? If not, what do i need to do to do so?

if ( $core_genius->inTimeSpan("mon - fri" )
   and $core_genius->inTimeSpan( "18:59 - 04:59" ) ) {
return xxx;
}

Many thanks!!

mikesoden
Beginner Modder
ranks
useravatar
Offline
1 Posts
Administrator has disabled public posting

Re: Users' time - Not Server time

Hi,

that will use the server time not the user's time, as the client does not send a GMT offset to the server so the server doesn't know that info about the client.

There are some ways to get around this though. One way is to run some JS on the client that sets a cookie with the current TZ in it. No doubt you can google for this. Then you can do some arithmetic in the MetaMod PHP code box and calculate the appropriate dates/times.

Note however that this requires a first page load to occur that runs the JS, and of course for that page load the MetaMod isn't going to know the results of the cookie. So the cookie would only be set for the 2nd and subsequent page loads. Would that be a problem for you?

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3328 Posts
User info in posts
Administrator has disabled public posting

Re: Users' time - Not Server time

Hi Mike,

Bearing in mind the limitations I mentioned in the forum, you can use the following JS to set the cookie. You could safely put this JS snippet onto every page:

var offset = new Date().getTimezoneOffset();
document.cookie="tzoffset=" + offset + "; expires=Fri, 31 Dec 2100 12:00:00 UTC";

As I mentioned, the 1st page that anyone requests will NOT receive the cookie, as it hasn’t been set yet (and that’s exceedingly annoying!). However, for subsequent page requests, MetaMod will be able to pick up this value and use it to calculate a TZ offset.

So in MetaMod, you could do this:

if ( isset($_COOKIE['tzoffset'] ) ) {
  $offset = (int)$_COOKIE['tzoffset'];
  $timestamp = time() - ($offset * 60); // imagine you are in GMT. then move to another part of the world, X hours ahead (tzoffset is -X). Then get the time of day using this new figure as the GMT date.
  $localtime = (int)gmdate('Gi', $timestamp); // from 0 (midnight) to 959 (9:59AM) to 1000 (10AM) to 2359
  if ($localtime >= 1600 and $localtime <= 1700) echo "it's between 4 and 5PM local time";
}


Instead of echoing something between those hours, you could instead return a module ID to be able to display that module. Please shout out if you need to detect particular days of the week etc as well as time of day, as that's a little more involved.

Voila! - local time zone support! (except for the 1st page requested…)

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3328 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1679
Total Polls:
6
Total Posts:
5933
Posts this week:
3
User Info:
 
Total Users:
4412
Newest User:
doyal54741
Members Online:
1
Guests Online:
149

Online: 
doyal54741

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked