Solved

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

Posted on 2014-11-22
5
244 Views
Last Modified: 2014-11-22
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?

Thanks
0
Comment
Question by:phaygarth
5 Comments
 
LVL 7

Expert Comment

by:Ray
ID: 40459531
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.

Required
http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Get visitor local time, sunrise and sunset time by IP with MaxMind GeoIP and PHP
http://snippets.khromov.se/get-visitor-local-time-sunrise-and-sunset-time-by-ip-with-maxmind-geoip-and-php/
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40459615
Just instantiate a javascript date object in your pages code.

now = new Date();
hour = now.getHours();
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40459772
See Practical Application #8 in this article:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

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
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40459942
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)

<body>

<script>
// 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";
    break;
  case 1: outURL = "bcd.php";
    break;
  case 2: outURL = "cde.php";
    break;
  case 3: outURL = "def.php";
    break;
  case 4: outURL = "efg.php";
    break;
  case 5: outURL = "fgh.php";
    break;
  case 6: outURL = "ghi.php";
    break;
  case 7: outURL = "hij.php";
    break;
  case 7: outURL = "ijk.php";
    break;
  case 9: outURL = "jkl.php";
    break;
  case 10: outURL = "klm.php";
    break;
  case 11: outURL = "lmn.php";
    break;
  case 12: outURL = "mno.php";
    break;
  case 13: outURL = "nop.php";
    break;
  case 14: outURL = "nop.php";
    break;
  case 15: outURL = "opq.php";
    break;
  case 16: outURL = "pqr.php";
    break;
  case 17: outURL = "qrs.php";
    break;
  case 18: outURL = "rst.php";
    break;
  case 19: outURL = "stu.php";
    break;
  case 20: outURL = "tuv.php";
    break;
  case 21: outURL = "uvw.php";
    break;
  case 22: outURL = "vwx.php";
    break;
  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>"); 
</script>
</body>

Open in new window


ask question if you need more info
0
 

Author Closing Comment

by:phaygarth
ID: 40460081
That worked perfectly, thank you.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now