Installation
Since this package is deprecated in favor of cocur/human-date, verify if the newer package meets your needs. If not, install via Composer:
composer require braincrafted/human-date-bundle
Register the bundle in config/bundles.php (Symfony):
return [
// ...
Braincrafted\HumanDateBundle\BraincraftedHumanDateBundle::class => ['all' => true],
];
First Use Case
Transform a \DateTime object into a human-readable string:
use Braincrafted\HumanDateBundle\Transformer\HumanDateTransformer;
$transformer = new HumanDateTransformer();
$date = new \DateTime('2023-10-15');
echo $transformer->transform($date); // Output: "October 15"
Twig Integration Enable the Twig extension in your templates:
{{ episode.airDate|humanDate }}
Requires the bundle to be registered and Twig configured.
Service Integration Register the transformer as a service in Symfony:
# config/services.yaml
services:
App\Services\HumanDateService:
arguments:
$transformer: '@braincrafted.human_date.transformer'
Use dependency injection in controllers:
public function show(HumanDateTransformer $transformer) {
$humanDate = $transformer->transform($this->getDate());
return response()->json(['date' => $humanDate]);
}
Custom Date Formatting Extend the transformer for project-specific rules:
class CustomHumanDateTransformer extends HumanDateTransformer {
public function transform(\DateTime $date) {
$result = parent::transform($date);
return str_replace('Today', 'Current Day', $result);
}
}
Register the custom service in services.yaml:
services:
App\Services\CustomHumanDateTransformer:
class: App\Services\CustomHumanDateTransformer
tags: ['human_date.transformer']
API Responses Use the transformer in JSON APIs for consistent date formatting:
return response()->json([
'created_at' => $transformer->transform($post->createdAt),
'updated_at' => $transformer->transform($post->updatedAt),
]);
Twig Filters
Extend Twig’s humanDate filter for dynamic behavior:
{% set customDate = episode.airDate|humanDate('custom_format') %}
Requires custom Twig extension logic (see Twig Extensions).
Deprecation Warning
This bundle is deprecated in favor of cocur/human-date. Evaluate migration if long-term maintenance is a priority.
braincrafted/human-date-bundle with cocur/human-date and update usage:
use Cocur\HumanDate\HumanDate;
$humanDate = new HumanDate();
echo $humanDate->format($date); // Output: "October 15"
Time Zone Sensitivity The transformer respects the system’s default time zone. Explicitly set time zones for consistency:
$date = new \DateTime('now', new \DateTimeZone('America/New_York'));
Caching Static Results Cache transformed dates in performance-critical applications (e.g., dashboard widgets):
$cacheKey = 'human_date_' . $date->getTimestamp();
$humanDate = Cache::remember($cacheKey, 3600, function() use ($transformer, $date) {
return $transformer->transform($date);
});
Locale Limitations
The bundle defaults to English. For multilingual apps, integrate with Symfony’s translation system or use cocur/human-date’s locale support:
$humanDate = new HumanDate('en_US');
Edge Cases Test with dates far in the past/future to ensure correct formatting:
$transformer->transform(new \DateTime('-50 years')); // "1973"
$transformer->transform(new \DateTime('+100 years')); // "2123"
Twig Debugging If the Twig filter fails, verify:
config/bundles.php.kernel.twig or framework.twig).Custom Logic Validation When extending the transformer, validate edge cases:
public function transform(\DateTime $date) {
$result = parent::transform($date);
if (strpos($result, 'Invalid') !== false) {
throw new \RuntimeException('Date transformation failed');
}
return $result;
}
Custom Rules
Override the getRules() method in a subclass to add project-specific logic:
protected function getRules() {
return array_merge(parent::getRules(), [
'custom_rule' => function($date) {
return $date->format('Y-m-d') === '2023-12-25' ? 'Christmas' : null;
},
]);
}
Integration with Carbon
If using Carbon, convert it to \DateTime first:
use Carbon\Carbon;
$date = Carbon::now();
$transformer->transform($date->toDateTime());
Symfony Form Types Use the transformer in form types for user-friendly date displays:
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'human_date' => function($date) {
return (new HumanDateTransformer())->transform($date);
},
]);
}
How can I help you explore Laravel packages today?