Skip to content

Instantly share code, notes, and snippets.

@ReeMii
Created November 3, 2013 20:49
Show Gist options
  • Save ReeMii/7294636 to your computer and use it in GitHub Desktop.
Save ReeMii/7294636 to your computer and use it in GitHub Desktop.
set time zone for MySQL connection in ZF2
<?php
function timezone_offset_string( $offset )
{
return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}
$offset = timezone_offset_get( new DateTimeZone( 'Europe/Warsaw' ), new DateTime() );
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
return new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$dbParams['database'].';host='.$dbParams['hostname'],
'database' => $dbParams['database'],
'username' => $dbParams['username'],
'password' => $dbParams['password'],
'hostname' => $dbParams['hostname'],
'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;SET time_zone = "'.timezone_offset_string($offset).'";')
));
},
),
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment