开发者

Compare user's time zone with the website's office location time zone

I am working on a project where i need to SHOW the visitor of the website a message in the contacts area like:

Contact no: +91-99-3241-5285 [You can call us now]

The message is highlighted in the above line, now my question is, how to compare the user's time zone with the w开发者_如何学Corking hours of the company's office.

P.S. - Suppose the company is in INDIA, and its working hours are 9.00am to 5.00pm.

Any suggestions?


Use javascript to determine the users timezone offset:

http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp

var d = new Date()
var gmtHours = -d.getTimezoneOffset()/60;
document.write("The local time zone is: GMT " + gmtHours);

Then calculate e.g. the current time in INDIA (GMT + 5:30 if i'm not wrong) and show "you can call us now" if the resulting time is within the working hours.

var diff = gmtHours - 5.5; // -> difference between users timezone and india in hours
var indiaDate = new Date();
indiaDate.setTime(d.getTime() - (diff * 60 * 60 * 1000));
document.write("<br />Current time in India: " + indiaDate.toString());

See the working demo at: http://jsfiddle.net/roberkules/RLsw9/


The replies immediately below your question are correct. There should be no need to know the client's time zone to calculate whether or not the current time is within hours of operation. It doesn't really matter whether it's currently 8:00am US Pacific, 11:00am US Eastern, or 11:00pm in Manila, Philippines. The important thing is what time it is relative to the business hours, not relative to the client.

Typically on all web servers I run, I set the server time to UTC. When people enter their hours, either have them enter it in GMT or, if you want them to be able to enter the hours in their local time, have a time zone list or combo box on the page. You can use PHP's DateTimeZone::listAbbreviations() function to help.

So, for example, say someone enters that their hours are from 9:00am to 5:00pm in the Asia/Calcutta time zone. When you're using the time for calculations, do something like this, assuming you have rows named something like hrs_start, hrs_end, and hrs_tz:

$hrs_tz = new DateTimeZone($row['hrs_tz']);
$hrs_start = new DateTime($row['hrs_start'], $hrs_tz);
$hrs_end = new DateTime($row['hrs_end'], $hrs_tz);

Now you can use these hours to calculate whether or not the hours are in or out of their business hours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜