hrevert / ht-time-zone
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.
Requires
- php: >=5.4
- zendframework/zend-modulemanager: ~2.1
- zendframework/zend-mvc: ~2.1
- zendframework/zend-servicemanager: ~2.1
- zendframework/zend-stdlib: ~2.1
- zendframework/zend-view: ~2.1
Requires (Dev)
Suggests
- doctrine/dbal: If you want to use dbal types provided this module
- zendframework/zend-filter: If you want to use the timezone converter(filters) provided this module
This package is auto-updated.
Last update: 2024-12-13 03:42:40 UTC
README
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).
Installation
- 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
Documentation
Doctrine Types
If you use doctrine DBAL, then you can use DBAL types provided this module which automate timezone conversion.
Included DBAL types
HtTimeZone\DBAL\Types\UTCDateTimeType
HtTimeZone\DBAL\Types\UTCTimeType
HtTimeZone\DBAL\Types\TimeZoneType
Usage
/** * @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', ], ] ], ], ];
Filters
This module comes with some filters related to timezone. If you don`t know filters, this should be the first one to read.
Included filters
- HtTimeZone\Filter\TimeZoneConverter
ClientToServerTimeZone
ServerToClientTimeZone
Usage
HtTimeZone\Filter\TimeZoneConverter
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.
HtTimeZone\Filter\ServerToClientTimeZone
$filter = $this->getServiceLocator()->get('FilterManager')->get('ServerToClientTimeZone'); echo $filter->filter(new DateTime());
HtTimeZone\Filter\ClientToServerTimeZone
$filter = $this->getServiceLocator()->get('FilterManager')->get('ClientToServerTimeZone'); echo $filter->filter(new DateTime());
Using View Helpers
This module comes with two view helpers to make our life easier.
- htTimeZone
- htTimeInterval
Basic Usage
htTimeZone
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
htTimeInterval
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`
Hydrator Strategies
HtTimeZone\Stdlib\Hydrator\Strategy\TimeZoneConverter
HtTimeZone\Stdlib\Hydrator\Strategy\TimeZoneStringStrategy
WIP
This is a work in progress. Use at your own risk!