[eluser]Jonas G[/eluser]
Hi all
This is my first attempt to contribute and also one of my first ever php classes so errors are bound to be found. Nevertheless I spent all night on this and I want to share and learn so here goes:
I needed a way of getting the timezone of a user based on their location. I wanted a way to fetch a "CI friendly" timezone to be used with the date helper (in the format of UM4/UTC/UP3...).
I ended up doing this by way of the GEO IP database. I realize there might be a shorter javascript way of doing this but for some reason this appealed to me. The downside of doing it this way is that it requires a few files. The upside is that this will
hopefully add some extra functionality as development continues.
So if you're alright with that, please pick up theese files and place them in your 'application' folder (they need to be in 'application/timezone/'):
http://subvision.dk/timezone.zip
library Timezone.php:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Timezone library
*
* @package default
* @author Jonas Grau
*/
class Timezone
{
var $gmt_timezone = 'Europe/London';
var $timezones = array(
'-12' => 'UM12',
'-11' => 'UM11',
'-10' => 'UM10',
'-9' => 'UM9',
'-8' => 'UM8',
'-7' => 'UM7',
'-6' => 'UM6',
'-5' => 'UM5',
'-4' => 'UM4',
'-3.5' => 'UM25',
'-3' => 'UM3',
'-2' => 'UM2',
'-1' => 'UM1',
'0' => 'UTC',
'1' => 'UP1',
'2' => 'UP2',
'3.5' => 'UP25',
'3' => 'UP3',
'4.5' => 'UP35',
'4' => 'UP4',
'4.5' => 'UP45',
'5' => 'UP5',
'6' => 'UP6',
'7' => 'UP7',
'8' => 'UP8',
'9.5' => 'UP85',
'9' => 'UP9',
'10' => 'UP10',
'11' => 'UP11',
'12' => 'UP11',
);
function __construct()
{
include_once(APPPATH."timezone/geoipcity.inc");
include_once(APPPATH."timezone/geoipregionvars.php");
include_once(APPPATH."timezone/tz.php");
}
function get_country_code_by_ip($ip)
{
$gi = geoip_open(APPPATH."timezone/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$ip);
geoip_close($gi);
return $record->country_code;
}
function get_region_by_ip($ip)
{
$gi = geoip_open(APPPATH."timezone/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$ip);
geoip_close($gi);
return $record->region;
}
function get_time_zone($cc, $rc)
{
return get_time_zone($cc, $rc);
}
function get_timezone_offset($from_tz, $to_tz)
{
return get_timezone_offset($from_tz, $to_tz)/(60*60);
}
function get_timezone_offset_by_ip($ip)
{
$gi = geoip_open(APPPATH."timezone/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$ip);
geoip_close($gi);
$local_timezone = $this->get_time_zone($record->country_code, $record->region);
$gmt_timezone = $this->gmt_timezone;
return $this->get_timezone_offset($gmt_timezone, $local_timezone);
}
function get_timezone_by_ip($ip)
{
return $this->timezones[$this->get_timezone_offset_by_ip($ip)];
}
}
/* End of file Timezone.php */
/* Location: ./system/application/libraries/Timezone.php */
I can now get the CI friendly timezone like so:
Controller:
Code:
function timezone()
{
$this->load->library('timezone');
echo $this->timezone->get_timezone_by_ip($this->input->ip_address());
}
I still have not tested this more than using my home IP and there is likely to be problems when IP is from places in UM25 and the like.
Please give me suggestions and comments.