HtTimeZone is a Zend Framework 2 module to simplify working with timezones. This module is useful when your application users are located all around the world. To use this module you will have to store all the dates in a fixed timezone(UTC is recommended).
- Add
"hrevert/ht-time-zone": "dev-master",
to your composer.json and runphp composer.phar update
- Enable the module in
config/application.config.php
- Copy file located in
./vendor/hrevert/ht-time-zone/config/ht-time-zone.global.php
to./config/autoload/ht-time-zone.global.php
and change the values as you wish
If you use doctrine DBAL, then you can use DBAL types provided this module which automate timezone conversion.
HtTimeZone\DBAL\Types\UTCDateTimeType
HtTimeZone\DBAL\Types\UTCTimeType
HtTimeZone\DBAL\Types\TimeZoneType
/**
* @Entity
* @Table(name="myEvent)
*/
class Event
{
...
/**
* @Column(type="UTCDateTime")
* @var DateTime
*/
protected $datetime;
...
}
Suppose, you want to store user's timezone;
<?php
/**
* @Entity
* @Table(name="user)
*/
class User
{
...
/**
* @Column(type="TimeZone")
* @var DateTimeZone
*/
protected $timeZone;
...
}
Suppose you want to store time in other timezone (NOT UTC), say Asia/Kathmandu
.
<?php
namespace Application\DBAL\Types;
use DateTimeZone;
class KtmDateTimeType extends AbstractTimeZoneDateTimeType
{
/**
* @var null|DateTimeZone
*/
static private $dateTimeZone = null;
/**
* {@inheritDoc}
*/
protected function getDateTimeZone()
{
return (self::$dateTimeZone) ? self::$dateTimeZone : (self::$dateTimeZone = new DateTimeZone('Asia/Kathmandu'));
}
}
return [
'doctrine' => [
'configuration' => [
'orm_default' => [
'types' => [
'ktmdatetime' => 'Application\DBAL\Types\KtmDateTimeType',
],
]
],
],
];
This module comes with some filters related to timezone. If you don`t know filters, this should be the first one to read.
- HtTimeZone\Filter\TimeZoneConverter
ClientToServerTimeZone
ServerToClientTimeZone
This filter is pretty simple.
$filter = new HtTimeZone\Filter\TimeZoneConverter($fromTimeZone, $toTimeZone);
echo $filter->filter(new DateTime());
To get more understanding of how this filter works, the best way is to dig the code here.
$filter = $this->getServiceLocator()->get('FilterManager')->get('ServerToClientTimeZone');
echo $filter->filter(new DateTime());
$filter = $this->getServiceLocator()->get('FilterManager')->get('ClientToServerTimeZone');
echo $filter->filter(new DateTime());
This module comes with two view helpers to make our life easier.
- htTimeZone
- htTimeInterval
This view helper sets timezone of a DateTime instance to that of client's timezone. Thats it! This view helper is useful to display date in client's timezone!
$dateTime = new DateTime();
echo $this->htTimeZone($dateTime)->format('d/m/Y H:i');// will print according to user`s timezone
This view helper is to display something like 5 minutes ago
.
echo $this->htTimeInterval(new DateTime()); // will print `Just Now`
echo $this->htTimeInterval(time() - 10); // will print `10 seconds ago`
HtTimeZone\Stdlib\Hydrator\Strategy\TimeZoneConverter
HtTimeZone\Stdlib\Hydrator\Strategy\TimeZoneStringStrategy
This is a work in progress. Use at your own risk!