Skip to content

Instantly share code, notes, and snippets.

@YasserElgammal
Last active September 11, 2025 14:26
Show Gist options
  • Select an option

  • Save YasserElgammal/0650466ee22d8e93a92b75921cea67f0 to your computer and use it in GitHub Desktop.

Select an option

Save YasserElgammal/0650466ee22d8e93a92b75921cea67f0 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Resources\Api;
use Illuminate\Http\Resources\Json\JsonResource;
class CreatedAtResource extends JsonResource
{
public function toArray($request)
{
return [
// Returns a human-friendly difference between now and the given date/time.
// Example: "2 hours ago", "3 days ago", "just now"
'time_human_readable' => $this?->diffForHumans(),
// Returns full date & time in standard "Y-m-d H:i:s" format.
// Example: "2024-03-11 14:30:00"
'date_time_string' => $this?->toDateTimeString(),
// Returns time in 12-hour format with AM/PM indicator.
// Example: "2:30 PM"
'time_12_hour' => $this?->isoFormat('h:mm A'),
// Returns short date with day number and full month name.
// Example: "11 March"
'date_short' => $this?->isoFormat('DD MMMM'),
// Returns medium date with day, month, and year.
// Example: "11 March 2024"
'date_medium' => $this?->isoFormat('D MMMM YYYY'),
// Returns long date with weekday, month, day with suffix, and year.
// Example: "Monday March 11th 2024"
'date_long' => $this?->isoFormat('dddd MMMM Do YYYY'),
// Using translatedFormat instead of format will localize month/day names.
// Example:
// - If locale = 'ar' → "11 مارس 2024"
// - If locale = 'en' → "11 March 2024"
'date_translated' => $this?->translatedFormat('j F Y'),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment