borsaco/jalali-date-time-bundle
Installation:
composer require borsaco/jalali-date-time-bundle
Register the bundle in AppKernel.php:
new Borsaco\JalaliDateTimeBundle\JalaliDateTimeBundle(),
First Use Case: Inject the service into a controller or service:
use Symfony\Component\DependencyInjection\ContainerInterface;
public function __construct(ContainerInterface $container) {
$this->jalaliDateTime = $container->get('jalali_date_time');
}
Format a Jalali date:
$this->jalaliDateTime->date('l j F Y H:i'); // e.g., "شنبه ۱۴۰۰/۰۱/۰۱ ۱۲:۳۰"
Date Formatting:
jalali_date_time->date() with format strings (e.g., 'Y/m/d' for ۱۴۰۰/۰۱/۰۱).$this->jalaliDateTime->date('Y/m/d', strtotime('2023-01-01'));
Timezone Handling:
$this->jalaliDateTime->date('l j F Y H:i T', false, false, false, 'Asia/Tehran');
Form Integration:
JalaliDateType in Symfony forms:
$builder->add('birthday', JalaliDateType::class);
bootstrap-persian-datetimepicker for frontend UI.Service Injection:
public function __construct(JalaliDateTime $jalaliDateTime) {
$this->jalaliDateTime = $jalaliDateTime;
}
Y/m/d) and convert on display.Deprecated Kernel:
Timezone Confusion:
Form Type Limitations:
JalaliDateType lacks built-in validation. Extend it or use constraints:
use Symfony\Component\Validator\Constraints\Date;
$builder->add('date', JalaliDateType::class, [
'constraints' => [new Date()]
]);
DatePicker Dependency:
bootstrap-persian-datetimepicker. Ensure JS/CSS assets are loaded.۱۴۰۰/۰۱/۰۱ = 2021-03-21).date_default_timezone_get() to debug timezone issues.try-catch for edge cases (e.g., invalid timestamps).Custom Formats: Extend the service to add methods for common formats:
public function jalaliShortDate($timestamp = null) {
return $this->date('Y/m/d', $timestamp);
}
Database Abstraction: Create a trait to handle Jalali ↔ Gregorian conversions in entities:
trait JalaliDateTrait {
public function getJalaliCreatedAt(): string {
return $this->jalaliDateTime->date('Y/m/d', $this->createdAt->getTimestamp());
}
}
Event Listeners:
Automate conversions in events (e.g., kernel.request):
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if ($request->has('gregorian_date')) {
$request->set('jalali_date', $this->jalaliDateTime->date('Y/m/d', $request->get('gregorian_date')));
}
}
How can I help you explore Laravel packages today?