58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Carbon\Carbon;
|
|
use DateTimeInterface;
|
|
|
|
class DateHelper
|
|
{
|
|
/**
|
|
* Format a date according to the current locale.
|
|
*
|
|
* Arabic: DD/MM/YYYY
|
|
* English: MM/DD/YYYY
|
|
*/
|
|
public static function formatDate(DateTimeInterface|string|null $date, ?string $locale = null): string
|
|
{
|
|
if ($date === null) {
|
|
return '';
|
|
}
|
|
|
|
$locale = $locale ?? app()->getLocale();
|
|
$format = $locale === 'ar' ? 'd/m/Y' : 'm/d/Y';
|
|
|
|
return Carbon::parse($date)->format($format);
|
|
}
|
|
|
|
/**
|
|
* Format a time in 12-hour format with AM/PM.
|
|
*/
|
|
public static function formatTime(DateTimeInterface|string|null $time, ?string $locale = null): string
|
|
{
|
|
if ($time === null) {
|
|
return '';
|
|
}
|
|
|
|
return Carbon::parse($time)->format('g:i A');
|
|
}
|
|
|
|
/**
|
|
* Format a datetime according to the current locale.
|
|
*
|
|
* Arabic: DD/MM/YYYY g:i A
|
|
* English: MM/DD/YYYY g:i A
|
|
*/
|
|
public static function formatDateTime(DateTimeInterface|string|null $datetime, ?string $locale = null): string
|
|
{
|
|
if ($datetime === null) {
|
|
return '';
|
|
}
|
|
|
|
$locale = $locale ?? app()->getLocale();
|
|
$format = $locale === 'ar' ? 'd/m/Y g:i A' : 'm/d/Y g:i A';
|
|
|
|
return Carbon::parse($datetime)->format($format);
|
|
}
|
|
}
|