Automatic time zone detection using JavaScript php:
Below is
index.php
{code type=codetype}
<!–www.moonreturn.com–>
<html>
<head>
<title>Automatic time zone detection using JavaScript</title>
<script language=’javascript’>
function calculate_time_zone() {
var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // jan 1st
var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(” “)-1));
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(” “)-1));
var std_time_offset = (jan1 – jan2) / (1000 * 60 * 60);
var daylight_time_offset = (june1 – june2) / (1000 * 60 * 60);
var dst;
if (std_time_offset == daylight_time_offset) {
dst = “0″; // daylight savings time is NOT observed
} else {
// positive is southern, negative is northern hemisphere
var hemisphere = std_time_offset – daylight_time_offset;
if (hemisphere >= 0)
std_time_offset = daylight_time_offset;
dst = “1″; // daylight savings time is observed
}
var i;
// check just to avoid error messages
if (document.getElementById(‘timezone’)) {
for (i = 0; i < document.getElementById(‘timezone’).options.length; i++) {
if (document.getElementById(‘timezone’).options[i].value == convert(std_time_offset)+”,”+dst) {
//alert(“Timezone=”+document.getElementById(‘timezone’).options[i].value);
document.form1.utc.value = document.getElementById(‘timezone’).options[i].value;
//document.form1.submit();
document.getElementById(‘timezone’).selectedIndex = i;
break;
}
}
}
}
function convert(value) {
var hours = parseInt(value);
value -= parseInt(value);
value *= 60;
var mins = parseInt(value);
value -= parseInt(value);
value *= 60;
var secs = parseInt(value);
var display_hours = hours;
// handle GMT case (00:00)
if (hours == 0) {
display_hours = “00″;
} else if (hours > 0) {
// add a plus sign and perhaps an extra 0
display_hours = (hours < 10) ? “+0″+hours : “+”+hours;
} else {
// add an extra 0 if needed
display_hours = (hours > -10) ? “-0″+Math.abs(hours) : hours;
}
mins = (mins < 10) ? “0″+mins : mins;
return display_hours+”:”+mins;
}
onload = calculate_time_zone;
</script>
</head>
<body>
<br /><br />
<center>
<h3>Automatic time zone detection using JavaScript</h3>
<select name=’timezone’ id=’timezone’ style=’width:350px; display:none;’>
<option value=’-12:00,0′>(-12:00) International Date Line West</option>
<option value=’-11:00,0′>(-11:00) Midway Island, Samoa</option>
<option value=’-10:00,0′>(-10:00) Hawaii</option>
<option value=’-09:00,1′>(-09:00) Alaska</option>
<option value=’-08:00,1′>(-08:00) Pacific Time (US & Canada)</option>
<option value=’-07:00,0′>(-07:00) Arizona</option>
<option value=’-07:00,1′>(-07:00) Mountain Time (US & Canada)</option>
<option value=’-06:00,0′>(-06:00) Central America, Saskatchewan</option>
<option value=’-06:00,1′>(-06:00) Central Time (US & Canada), Guadalajara, Mexico city</option>
<option value=’-05:00,0′>(-05:00) Indiana, Bogota, Lima, Quito, Rio Branco</option>
<option value=’-05:00,1′>(-05:00) Eastern time (US & Canada)</option>
<option value=’-04:00,1′>(-04:00) Atlantic time (Canada), Manaus, Santiago</option>
<option value=’-04:00,0′>(-04:00) Caracas, La Paz</option>
<option value=’-03:30,1′>(-03:30) Newfoundland</option>
<option value=’-03:00,1′>(-03:00) Greenland, Brasilia, Montevideo</option>
<option value=’-03:00,0′>(-03:00) Buenos Aires, Georgetown</option>
<option value=’-02:00,1′>(-02:00) Mid-Atlantic</option>
<option value=’-01:00,1′>(-01:00) Azores</option>
<option value=’-01:00,0′>(-01:00) Cape Verde Is.</option>
<option value=’00:00,0′>(00:00) Casablanca, Monrovia, Reykjavik</option>
<option value=’00:00,1′>(00:00) GMT: Dublin, Edinburgh, Lisbon, London</option>
<option value=’+01:00,1′>(+01:00) Amsterdam, Berlin, Rome, Vienna, Prague, Brussels</option>
<option value=’+01:00,0′>(+01:00) West Central Africa</option>
<option value=’+02:00,1′>(+02:00) Amman, Athens, Istanbul, Beirut, Cairo, Jerusalem</option>
<option value=’+02:00,0′>(+02:00) Harare, Pretoria</option>
<option value=’+03:00,1′>(+03:00) Baghdad, Moscow, St. Petersburg, Volgograd</option>
<option value=’+03:00,0′>(+03:00) Kuwait, Riyadh, Nairobi, Tbilisi</option>
<option value=’+03:30,0′>(+03:30) Tehran</option>
<option value=’+04:00,0′>(+04:00) Abu Dhadi, Muscat</option>
<option value=’+04:00,1′>(+04:00) Baku, Yerevan</option>
<option value=’+04:30,0′>(+04:30) Kabul</option>
<option value=’+05:00,1′>(+05:00) Ekaterinburg</option>
<option value=’+05:00,0′>(+05:00) Islamabad, Karachi, Tashkent</option>
<option value=’+05:30,0′>(+05:30) Chennai, Kolkata, Mumbai, New Delhi, Sri Jayawardenepura</option>
<option value=’+05:45,0′>(+05:45) Kathmandu</option>
<option value=’+06:00,0′>(+06:00) Astana, Dhaka</option>
<option value=’+06:00,1′>(+06:00) Almaty, Nonosibirsk</option>
<option value=’+06:30,0′>(+06:30) Yangon (Rangoon)</option>
<option value=’+07:00,1′>(+07:00) Krasnoyarsk</option>
<option value=’+07:00,0′>(+07:00) Bangkok, Hanoi, Jakarta</option>
<option value=’+08:00,0′>(+08:00) Beijing, Hong Kong, Singapore, Taipei</option>
<option value=’+08:00,1′>(+08:00) Irkutsk, Ulaan Bataar, Perth</option>
<option value=’+09:00,1′>(+09:00) Yakutsk</option>
<option value=’+09:00,0′>(+09:00) Seoul, Osaka, Sapporo, Tokyo</option>
<option value=’+09:30,0′>(+09:30) Darwin</option>
<option value=’+09:30,1′>(+09:30) Adelaide</option>
<option value=’+10:00,0′>(+10:00) Brisbane, Guam, Port Moresby</option>
<option value=’+10:00,1′>(+10:00) Canberra, Melbourne, Sydney, Hobart, Vladivostok</option>
<option value=’+11:00,0′>(+11:00) Magadan, Solomon Is., New Caledonia</option>
<option value=’+12:00,1′>(+12:00) Auckland, Wellington</option>
<option value=’+12:00,0′>(+12:00) Fiji, Kamchatka, Marshall Is.</option>
<option value=’+13:00,0′>(+13:00) Nuku’alofa</option>
</select>
<form name=”form1″ method=”POST” action=”index1.php”>
<input type=”hidden” name=”utc” id=”utc”>
<input type=”submit” name=”submit” value=”ClickForDetection”>
</form>
</center>
<!–<script type=”text/javascript” language=”JavaScript”>
//submit form
document.form1.submit();
</script>
–>
</body>
</html>
{/code}
index1.php
{code type=codetype}
<?php
//print_r($_REQUEST);
$utc = explode(“,”, $_REQUEST['utc']);
//echo “<br>utc0=”.$utc[0];
if($utc[0]==”-12:00″)
{
$timeZone = “Kwajalein”;
}
elseif($utc[0]==”-11:00″)
{
$timeZone = “Pacific/Samoa”;
}
elseif($utc[0]==”-10:00″)
{
$timeZone = “Pacific/Honolulu”;
}
elseif($utc[0]==”-09:00″)
{
$timeZone = “America/Anchorage”;
}
elseif($utc[0]==”-08:00″)
{
$timeZone = “America/Los_Angeles”;
}
elseif($utc[0]==”-07:00″)
{
$timeZone = “America/Denver”;
}
elseif($utc[0]==”-06:00″)
{
$timeZone = “America/Regina”;
}
elseif($utc[0]==”-05:00″)
{
$timeZone = “America/New_York”;
}
elseif($utc[0]==”-04:00″)
{
$timeZone = “America/Halifax”;
}
elseif($utc[0]==”-03:00″)
{
$timeZone = “America/St_Johns”;
}
elseif($utc[0]==”-02:00″)
{
$timeZone = “Atlantic/South_Georgia”;
}
elseif($utc[0]==”-01:00″)
{
$timeZone = “Atlantic/Azores”;
}
elseif($utc[0]==”00:00″)
{
$timeZone = “Europe/London”;
}
elseif($utc[0]==”+01:00″)
{
$timeZone = “Europe/Belgrade”;
}
elseif($utc[0]==”+02:00″)
{
$timeZone = “Europe/Minsk”;
}
elseif($utc[0]==”+03:00″)
{
$timeZone = “Asia/Kuwait”;
}
elseif($utc[0]==”+04:00″)
{
$timeZone = “Asia/Muscat”;
}
elseif($utc[0]==”+05:00″)
{
$timeZone = “Asia/Yekaterinburg”;
}
elseif($utc[0]==”+05:30″)
{
$timeZone = “Asia/Calcutta”;
}
elseif($utc[0]==”+05:45″)
{
$timeZone = “Asia/Katmandu”;
}
elseif($utc[0]==”+06:00″)
{
$timeZone = “Asia/Dhaka”;
}
elseif($utc[0]==”+07:00″)
{
$timeZone = “Asia/Krasnoyarsk”;
}
elseif($utc[0]==”+08:00″)
{
$timeZone = “Asia/Brunei”;
}
elseif($utc[0]==”+09:00″)
{
$timeZone = “Asia/Yakutsk”;
}
elseif($utc[0]==”+10:00″)
{
$timeZone = “Australia/Sydney”;
}
elseif($utc[0]==”+11:00″)
{
$timeZone = “Asia/Magadan”;
}
elseif($utc[0]==”+12:00″)
{
$timeZone = “Pacific/Fiji”;
}
elseif($utc[0]==”+13:00″)
{
$timeZone = “Pacific/Tongatapu”;
}
//echo “<br>TimeZone=”.$timeZone;
?>
<?PHP
putenv(“TZ=$timeZone”);
//echo “Present time in (India IST) is : “.date(“d D, M Y – h:i:s A”);
echo “<br>Present time in ($timeZone IST) is= : “.date(“Y-m-d h:i:s A”);
?>{/code}
Enjoy…..
Here in index1.php
I have wrtten code like elseif($utc[0]==”-11:00″)
{
$timeZone = “Pacific/Samoa”;
}
you can customized/add more time zones .