-
Notifications
You must be signed in to change notification settings - Fork 7.6k
convert to gmt
This helper is an extension to the date_helper. It takes a supplied timestamp, a timezone value, and a daylight savings value as inputs and returns a the time in GMT. The timezone value should be formatted as UM8, etc. as described in the date helper documentation.
The reason for this addition to the date helper was that I was having some frustrations when trying to create a scheduling function. I needed to allow users to input a time which would be converted into GMT before being input into the db. This way, when another user in a different timezone viewed the scheduled event (stored in GMT), it could be easily converted into their local time using the existing gmt_to_local() function.
I thought that the local_to_gmt() function would work like the gmt_to_local() function but it turns out that it doesn't for some reason. The local_to_gmt() function takes a given time, and converts to GMT based on the server's timezone, not a given timezone.
This addition to the Date helper is simply the exact opposite of gmt_to_local().
- Copy the code below and paste it into /application/helpers/MY_date_helper.php file.
// ------------------------------------------------------------------------
/**
* Converts LOCAL time to a GMT value when given a timezone and a timestamp
*
* Takes a Unix timestamp (in LOCAL TIME) as input, and returns
* at the GMT value based on the timezone and DST setting
* submitted
*
* @access public
* @param integer Unix timestamp
* @param string timezone
* @param bool whether DST is active
* @return integer
*/
function convert_to_gmt($time = '', $timezone = 'UTC', $dst = FALSE)
{
if ($time == '')
{
return now();
}
$time -= timezones($timezone) * 3600;
if ($dst == TRUE)
{
$time -= 3600;
}
return $time;
}
In your controller:
$timezone = 'UM8';
$local_timestamp = '1140153693';
$daylight_saving = TRUE;
$gmt_conversion = convert_to_gmt($local_timestamp, $timezone, $daylight_saving);
And voila! You have your time in GMT.