parfum/app/Helpers/DateTimeHelpers.php
2025-09-29 11:44:11 +07:00

64 lines
1.6 KiB
PHP

<?php
use Carbon\Carbon;
if (! function_exists('formatDate')) {
function formatDate(?string $date = null, ?string $format = 'l, d M Y')
{
return ! $date ? '-' : Carbon::parse($date)->translatedFormat($format);
}
}
if (! function_exists('formatTime')) {
function formatTime(?string $time = null, ?string $format = 'H:i')
{
return ! $time ? '-' : Carbon::parse($time)->format($format);
}
}
if (! function_exists('formatDateTime')) {
function formatDateTime(string $dateTime)
{
return Carbon::parse($dateTime)->translatedFormat('l, d M Y H:i:s');
}
}
if (! function_exists('formatDateDash')) {
function formatDateDash(string $date)
{
return Carbon::parse($date)->translatedFormat('Y-m-d');
}
}
if (! function_exists('getAge')) {
function getAge(string $birthdate)
{
$birth = Carbon::parse($birthdate);
$now = Carbon::now();
$diff = $birth->diff($now);
return "{$diff->y} tahun {$diff->m} bulan";
}
}
if (! function_exists('timeAgo')) {
function timeAgo(?string $date = null)
{
return ! $date ? '-' : Carbon::parse($date)->diffForHumans();
}
}
if (! function_exists('normalizeOpeningHours')) {
function normalizeOpeningHours(array $openingHours)
{
$days = ['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu', 'minggu'];
return collect($days)
->mapWithKeys(fn ($day) => [
$day => $openingHours[$day] ?? ['open' => null, 'close' => null],
])
->toArray();
}
}