Reader Stacks

Working With Dates in Laravel: Current Date, Timezone, and Human-Readable Formats

diffForHumans() is what turns a raw timestamp into '3 hours ago' — a small but genuinely important detail for how approachable a UI's dates feel compared to a raw ISO string.

Working With Dates in Laravel: Current Date, Timezone, and Human-Readable Formats

Carbon, bundled directly into Laravel via the now() helper and every model's date attributes, handles the common date needs — getting the current date/day, working across timezones, and formatting a timestamp into a human-readable relative phrase like "3 hours ago."

Getting the current date and time

$now = now(); // a Carbon instance for the current moment

echo $now->toDateString();     // 2026-05-07
echo $now->toDateTimeString(); // 2026-05-07 14:30:00
echo $now->format('l, F j, Y'); // Thursday, May 7, 2026

Getting just the current day name

echo now()->format('l'); // "Thursday"
echo now()->dayName; // "Thursday" — equivalent property-style access

Getting today's records with a date-only comparison

$todaysOrders = Order::whereDate('created_at', today())->get();

whereDate() compares only the date portion of a datetime column, ignoring the time — this is exactly what makes it correct for "today's records," since a plain where('created_at', today()) would fail to match any row whose timestamp includes a specific time component, which is virtually always the case.

Getting records within a date range

$orders = Order::whereBetween('created_at', [now()->subDays(7), now()])->get();

Working with timezones

$now = now('America/New_York');
echo $now->format('Y-m-d H:i:s T');

Passing a timezone identifier directly to now() returns the current moment expressed in that timezone — genuinely important to get right for an application serving users across multiple timezones, where a date displayed in the server's own default timezone can be meaningfully wrong for the actual viewing user.

Building a timezone select list for a user preference form

PHP's own timezone_identifiers_list() returns every valid timezone identifier (like America/New_York, Europe/London) — no third-party package is needed just to build this list, since it's a native PHP function already available in any environment.

Converting a stored UTC timestamp to a user's local timezone for display

{{ $order->created_at->setTimezone($user->timezone)->format('M j, Y g:i A') }}

Storing every timestamp in UTC in the database (Laravel's default) and converting only at display time, per the viewing user's own preferred timezone, is the standard and most reliable approach — converting and storing timestamps in a specific timezone up front creates ambiguity and bugs the moment the application needs to serve users in more than one timezone.

Human-readable relative dates

echo $post->created_at->diffForHumans(); // "3 hours ago", "2 days ago", "in 5 minutes"

diffForHumans() is what turns a raw timestamp into an approachable relative phrase — a small but genuinely significant detail for how a UI feels to actual users, since "3 hours ago" reads far more naturally at a glance than a raw ISO datetime string like "2026-05-07T11:30:00Z".

Comparing diffForHumans() output against a specific reference point

echo $post->published_at->diffForHumans($post->updated_at); // relative to updated_at, not now()

Passing a second argument compares against that specific point in time instead of the current moment — useful for something like showing how long after publishing a post was last edited, rather than always comparing against "now."