Global Date Format
Fri Oct 06 2023 10:59:32 GMT+0000 (Coordinated Universal Time)
Saved by @mradul
<?php namespace App\Traits; use Carbon\Carbon; use App\Models\User; trait GlobalDateFormat { public function GlobalDateFormat($date) { $ipaddress = User::getClientIP(); $details = User::ip_details($ipaddress); $dateFormats = [ '1' => 'F j, Y h:i A', // January 15, 2023 03:30 PM '2' => 'M j, Y h:i A', // Jan 15, 2023 03:30 PM '3' => 'Y-m-d H:i:s', // 2023-01-15 15:30:00 '4' => 'm/d/Y g:i A', // 01/15/2023 3:30 PM '5' => 'D, M j, Y g:i A', // Sun, Jan 15, 2023 3:30 PM '6' => 'd M Y H:i:s', // 15 Jan 2023 15:30:00 '7' => 'Y-m-d', // 2023-01-15 (Date only) '8' => 'h:i A', // 03:30 PM (Time only) '9' => 'Y', // 2023 (Year only) '10' => 'M j', // Jan 15 (Month and day without year) '11' => 'M j, Y' // Jan 15, 2023 ]; $formats = []; foreach($dateFormats as $key => $value) { $formats[$key] = Carbon::parse($date)->timezone($details['timezone'])->format($value); } return $formats; } public function lastSeenTime($date) { $ipaddress = User::getClientIP(); $details = User::ip_details($ipaddress); $lastSeenDateTime = Carbon::parse($date); $timeDiffInSeconds = $lastSeenDateTime->diffInSeconds(now()); $timeDiffInMinutes = $lastSeenDateTime->diffInMinutes(now()); $timeDiffInHours = $lastSeenDateTime->diffInHours(now()); $timeDiffInDays = $lastSeenDateTime->diffInDays(now()); $formattedDate = $lastSeenDateTime->timezone($details['timezone'])->format('F j, Y h:i A'); if ($timeDiffInSeconds < 60) { return $timeDiffInSeconds . ' seconds ago'; } elseif ($timeDiffInMinutes < 60) { return $timeDiffInMinutes . ' minutes ago'; } elseif ($timeDiffInHours < 24) { return $timeDiffInHours . ' hours ago'; } else { return $timeDiffInDays . ' days ago'; } } }
Comments