Display different URL/text to visitor based on their local time.

I would like to display a different, clickable URL to my website visitors depending on their local time.

So for example, if they visit my site and their local time is between 7pm to 6am, the page should display 'abc.com'.

Whereas if they visit between 6am to 7pm, I would like to display 'xyz.com' to them.

Is this possible and how would I go about it please?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RayDirector of Software DevelopmentCommented:
Time zone information of the browser is not part of the HTTP spec, so you can't just get it from a header.  You'll probably need to get geo info from their IP.


Get visitor local time, sunrise and sunset time by IP with MaxMind GeoIP and PHP
Neil RussellTechnical Development LeadCommented:
Just instantiate a javascript date object in your pages code.

now = new Date();
hour = now.getHours();
Ray PaseurCommented:
See Practical Application #8 in this article:

Unless you really need to keep this information in PHP, the best answer is probably something like this:

1. Send "Good morning" and "Good afternoon" messages in hidden divs
2. Detect the time of day
3. Show the div that is appropriate
greetings  phaygarth, , The local user's browser is the best place to get the users "TIME - DATE" for the area timezone that they are located in, , I would use the javascript of the browser to get the hour from the Date() object, there are 24 separate hour designations from 0 (midnight) to 23 (11 pm), I do not see that it would be a practical operation for your site to have 24 different URL, one for each hour, but here is the javascript i used in fire fox to do that , this will change a string to 24 different URL as hour1 is 0 to 23,
the   document.write( ) used here MUST be below the <body> tag for correct html placement, so I placed the body tag in this, but it is irrelevant  to the javascript used below, except as where the output is written (placement)


// Array below is here if you need display of the time slot
var timeArray = ["12 am","1 am","2 am","3 am","4 am","5 am","6 am","7 am",
  "8 am","9 am","10 am","11 am","12 pm","1 pm","2 pm","3 pm","4 pm",
"5 pm","6 pm","7 pm","8 pm","9 pm","10 pm","11 pm"];

//  hour1 will have a Number not a string
var hour1 = new Date().getHours();

// if some javascript framework changes
// the Date() getHours() it may not work as expected,
// so include an ERROR PAGE or DEFAULT URL below
var outURL = "error_page.php";

// a switch makes the best use of code work for so many selections
switch (hour1){
  case 0: outURL = "abc.php";
  case 1: outURL = "bcd.php";
  case 2: outURL = "cde.php";
  case 3: outURL = "def.php";
  case 4: outURL = "efg.php";
  case 5: outURL = "fgh.php";
  case 6: outURL = "ghi.php";
  case 7: outURL = "hij.php";
  case 7: outURL = "ijk.php";
  case 9: outURL = "jkl.php";
  case 10: outURL = "klm.php";
  case 11: outURL = "lmn.php";
  case 12: outURL = "mno.php";
  case 13: outURL = "nop.php";
  case 14: outURL = "nop.php";
  case 15: outURL = "opq.php";
  case 16: outURL = "pqr.php";
  case 17: outURL = "qrs.php";
  case 18: outURL = "rst.php";
  case 19: outURL = "stu.php";
  case 20: outURL = "tuv.php";
  case 21: outURL = "uvw.php";
  case 22: outURL = "vwx.php";
  case 23: outURL = "wxy.php";

// the line below  can be in a separate <scritp> section if nessary
document.write("clickable URL for "+timeArray[hour1]+"- <a href='"+outURL+"'>"+outURL+"</a>"); 

Open in new window

ask question if you need more info

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phaygarthAuthor Commented:
That worked perfectly, thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.