now() and today()

now() returns the current moment as a Carbon date object.

Syntaxnow()->format('Y-m-d');

The now() helper returns a Carbon instance for the current date and time. today() returns the current date at midnight. Because they are Carbon objects, you can format and manipulate them fluently.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • A controller stores the current timestamp in a record's 'published_at' column using now() without importing Carbon explicitly.
  • A query scope filters records created within the last 24 hours by comparing 'created_at' to now()->subDay().
  • A scheduled job sets a reminder to fire exactly one week from the current moment by passing now()->addWeek() to the job's delay.

More examples

Get and format the current time

now() returns a Carbon instance for the current moment, providing all of Carbon's formatting methods without importing the class.

Example · php
$now = now();

$now->toDateString();      // '2025-03-14'
$now->toDateTimeString();  // '2025-03-14 09:30:00'
$now->format('D, d M Y'); // 'Fri, 14 Mar 2025'

Relative time arithmetic

Chains Carbon arithmetic methods onto now() to compute future or past timestamps for expiry dates, cutoff filters, and delay scheduling.

Example · php
$expiry   = now()->addDays(30);    // 30 days from now
$cutoff   = now()->subHours(6);    // 6 hours ago
$nextWeek = now()->addWeek();      // same time, one week later

// Use in a query
$recentOrders = Order::where('created_at', '>=', now()->subDay())->get();

Publish an article with now()

Sets the published_at timestamp to the exact current moment and echoes it in the flash message using the same now() call.

Example · php
public function publish(Post $post): RedirectResponse
{
    $post->update([
        'published_at' => now(),
        'status'       => 'published',
    ]);

    return redirect()->route('posts.show', $post)
        ->with('success', 'Post published at ' . now()->toTimeString());
}

Discussion

  • Be the first to comment on this lesson.