Return to Snippet

Revision: 19018
at October 14, 2009 16:53 by terrencewood


Initial Code
/**
 * Implements hook_views_query_alter
 * - If the db doesn't have timezone support transform exposed filter date inputs from the sites default timezone to UTC.
 * @TODO - can the conversion to UTC happen somewhere in the $view->handler, hook_form_alter, or date api
 */

function mymodule_views_query_alter(&$view, &$query) {
	
	if(isset($query->where['date']['clauses']) && !variable_get('date_db_tz_support', 0)) {
		$tz = variable_get('date_default_timezone_name', 'Pacific/Auckland');
		date_default_timezone_set('UTC');
		$dates = &$query->where['date']['clauses'];
		foreach($dates as &$date) {
			$date = preg_replace_callback(
				'(\d{4}-\d{2}-\d{2})',
				create_function('$date','return strftime("%Y-%m-%d %H:%M:%S", strtotime($date[0]." '.$tz.'"));'),
				$date
			);
		}
		date_default_timezone_set($tz);
	}
}

Initial URL


Initial Description


Initial Title
Adjust date field inputs to UTC

Initial Tags
date, drupal

Initial Language
PHP