Installation:
composer require assoconnect/php-date-bundle
Ensure your config/bundles.php includes:
Assoconnect\PhpDateBundle\PhpDateBundle::class => ['all' => true],
First Use Case:
use Assoconnect\PhpDate\AbsoluteDate;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Event
{
#[ORM\Column(type: AbsoluteDateType::NAME)]
private AbsoluteDate $date;
}
{{ event.date|date('Y-m-d') }} {# Renders as "2024-02-14" #}
Key Files to Review:
config/packages/assoconnect_php_date.yaml (default config)src/Assoconnect/PhpDateBundle/DependencyInjection/ (for advanced config)$absoluteDate = new AbsoluteDate('2024-02-14T12:00:00+02:00');
$entityManager->persist($event); // Automatically converts to DB storage format
$qb->andWhere('e.date > :minDate')
->setParameter('minDate', new AbsoluteDate('2024-01-01'));
$normalizer = new AbsoluteDateNormalizer();
$normalized = $normalizer->normalize($absoluteDate, null, [
'format' => 'Y-m-d\TH:i:sP'
]);
// Returns: "2024-02-14T12:00:00+02:00"
$denormalizer = new AbsoluteDateNormalizer();
$date = $denormalizer->denormalize('2024-02-14', null, [
'format' => 'Y-m-d'
]);
{{ event.date|date('d/m/Y') }} {# "14/02/2024" #}
{{ event.date|timezone('America/New_York')|date('H:i') }} {# Localized time #}
// src/Twig/Extension/CustomDateExtension.php
class CustomDateExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters(): array
{
return [
new \Twig\TwigFilter('is_future', [$this, 'isFutureDate']),
];
}
public function isFutureDate(AbsoluteDate $date): bool
{
return $date > new AbsoluteDate();
}
}
AbsoluteDateClockuse Assoconnect\PhpDateBundle\Clock\AbsoluteDateClock;
$clock = new AbsoluteDateClock(new \DateTimeImmutable('2024-02-15'));
$this->container->set('assoconnect_php_date.clock', $clock);
// Now all AbsoluteDate instances will use 2024-02-15 as "now"
use Assoconnect\PhpDateBundle\Translatable\AbsoluteDateTranslatable;
#[ORM\Entity]
class MultilingualEvent
{
#[ORM\Column(type: AbsoluteDateType::NAME)]
#[AbsoluteDateTranslatable]
private AbsoluteDate $date;
}
Database Schema:
AbsoluteDateType stores dates as ISO-8601 strings (e.g., "2024-02-14T12:00:00+02:00"). Ensure your DB supports TEXT or VARCHAR columns.Clock Service:
Clock service in config/services.yaml:
services:
assoconnect_php_date.clock:
class: Assoconnect\PhpDateBundle\Clock\AbsoluteDateClock
arguments:
$clock: '@clock' # Symfony's default Clock
Validation:
assoconnect/validator-bundle for custom validation:
use Assoconnect\Validator\Constraints\AbsoluteDate;
#[ORM\Entity]
class Event
{
#[ORM\Column(type: AbsoluteDateType::NAME)]
#[AbsoluteDate('future')]
private AbsoluteDate $date;
}
API Responses:
AbsoluteDateNormalizer in API platforms:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
normalization_context:
groups: ['api']
date_format: 'Y-m-d\TH:i:sP'
Time Zone Handling:
AbsoluteDate uses the system default timezone if none is specified.$date = new AbsoluteDate('2024-02-14T12:00:00', 'Europe/Paris');
Doctrine Hydration:
DATETIME instead of TEXT, Doctrine may fail to hydrate.AbsoluteDateType in config/packages/doctrine.yaml:
doctrine:
dbal:
types:
AbsoluteDate:
class: Assoconnect\PhpDate\Doctrine\AbsoluteDateType
comment: "Stores dates as ISO-8601 strings"
Twig Caching:
php bin/console cache:clear --env=prod
PHP 8.4+ Changes:
^7.0 and assoconnect/php-date:^2.11.Null Values:
null by default (pre-2.0.1 behavior).null:
$normalizer->setIgnoredAttributes(['null']);
Clock Debugging:
use Assoconnect\PhpDate\AbsoluteDate;
public function debugClock(): void
{
$now = new AbsoluteDate();
dump($now->format('Y-m-d H:i:sP')); // Check timezone/offset
}
Doctrine Type Issues:
php bin/console debug:doctrine
AbsoluteDate under "Custom Types."Twig Filter Not Found:
AbsoluteDateExtension is loaded. Check var/cache/dev/Twig* for errors.Denormalization Failures:
$denormalizer->denormalize('invalid-date', null, ['format' => 'Y-m-d']);
// Throws: \Assoconnect\PhpDate\Exception\InvalidDateException
Custom Doctrine Types:
AbsoluteDateType for domain-specific logic:
class CustomAbsoluteDateType extends AbsoluteDateType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value instanceof AbsoluteDate) {
return $value->format('Y-m-d H:i:s.uP'); // Custom format
}
return parent::convertToDatabaseValue($value, $platform);
}
}
Clock Decorators:
AbsoluteDateClock to add logic:
use Symfony\Component\Clock\Clock;
class LoggingClockDecorator implements Clock
{
public function __construct(private Clock $clock) {}
public function now(): \DateTimeInterface
How can I help you explore Laravel packages today?