Skip to content

Dates and Time

Utilities

Dates and Time

Introduction

Web applications constantly work with dates and timestamps, from displaying when a post was published to expiring invitations and trials. Dates in Laravel are the standard library's time values, so they work with every Go library you already use, and you don't need a separate dependency.

Most application code works with three types:

Type Use it for
time.Time A moment paired with a location, used for formatting, comparisons, and storage (date.Now returns one).
time.Duration A length of time, such as a cache entry's lifetime or a job's delay.
date.Date A calendar date without a time of day, such as an invoice's due date.

The date package adds what the standard library leaves out: the current time on a clock your tests can move, checks such as date.IsPast and date.IsToday, month arithmetic that stops at the end of a short month, and relative times such as "3 minutes ago".

The current time

date.Now returns the current time as a time.Time. It reads the application's clock from the context you pass, which in a handler is r, and in a command or a job is its ctx:

1import "go.laravel.com/date"
2 
3now := date.Now(r)
4 
5now.Year() // 2026
6now.Month() // October
7now.Weekday() // Monday
1import "go.laravel.com/date"
2 
3now := date.Now(r)
4 
5now.Year() // 2026
6now.Month() // October
7now.Weekday() // Monday

Always read the time with date.Now, not the standard library's time.Now, so your tests can move the clock. For today's date without a time, call date.Today, which returns a date.Date in the server's location, usually UTC. For today's date in a user's location, call date.TodayIn(r, chicago) instead.

Timezones

Laravel has no application-wide timezone setting. date.Now returns the time in the server's location, which is usually UTC. Keep your dates in UTC while you calculate and store them, and convert them with the time's In method when you show a user their local time:

1chicago, err := time.LoadLocation("America/Chicago")
2if err != nil {
3 return web.Fail(err)
4}
5 
6local := post.CreatedAt.In(chicago)
1chicago, err := time.LoadLocation("America/Chicago")
2if err != nil {
3 return web.Fail(err)
4}
5 
6local := post.CreatedAt.In(chicago)

Laravel includes the IANA time zone database in your application's binary, so names such as America/Chicago load even on a server or container that doesn't have one installed.

Formatting dates

To show a date, call its Format method with a layout. A layout is the date January 2, 2006 at 3:04:05 PM, written the way you'd like your date to look:

1now := date.Now(r)
2 
3now.Format("January 2, 2006") // October 5, 2026
4now.Format("Jan 2, 15:04") // Oct 5, 14:30
5now.Format("Monday, 3:04 PM") // Monday, 2:30 PM
6now.Format(time.DateOnly) // 2026-10-05
1now := date.Now(r)
2 
3now.Format("January 2, 2006") // October 5, 2026
4now.Format("Jan 2, 15:04") // Oct 5, 14:30
5now.Format("Monday, 3:04 PM") // Monday, 2:30 PM
6now.Format(time.DateOnly) // 2026-10-05

The time package's documentation lists every layout element and the predefined layouts, such as time.DateOnly and time.RFC3339. A time formats in its own location, so convert it with In first to show a user's local time.

Dates in views

In templates, the date function formats a time with the same layouts, and the forHumans function describes it relative to now, as date.ForHumans does in Go code. Without a layout, date renders times like October 5, 2026. Times display in their own location unless you give a location's name after the layout:

1{{/* October 5, 2026 */}}
2<time datetime="{{date "2006-01-02T15:04:05Z07:00" .CreatedAt}}">{{date .CreatedAt}}</time>
3 
4{{date "Jan 2, 15:04" .CreatedAt}} {{/* Oct 5, 14:30 */}}
5{{date "Jan 2, 15:04" "America/Chicago" .CreatedAt}} {{/* Oct 5, 09:30 */}}
6{{forHumans .CreatedAt}} {{/* 3 minutes ago */}}
1{{/* October 5, 2026 */}}
2<time datetime="{{date "2006-01-02T15:04:05Z07:00" .CreatedAt}}">{{date .CreatedAt}}</time>
3 
4{{date "Jan 2, 15:04" .CreatedAt}} {{/* Oct 5, 14:30 */}}
5{{date "Jan 2, 15:04" "America/Chicago" .CreatedAt}} {{/* Oct 5, 09:30 */}}
6{{forHumans .CreatedAt}} {{/* 3 minutes ago */}}

Date arithmetic

To find the time two hours from now, call the time's Add method with a time.Duration. For days, call AddDate instead, so adding a day across a daylight saving change keeps the same time of day. Each returns a new time and leaves the original as it was:

1now := date.Now(r)
2 
3now.Add(2*time.Hour + 30*time.Minute)
4now.Add(-15 * time.Minute)
5now.AddDate(0, 0, 7) // A week from now
6now.AddDate(0, 0, -3) // Three days ago
1now := date.Now(r)
2 
3now.Add(2*time.Hour + 30*time.Minute)
4now.Add(-15 * time.Minute)
5now.AddDate(0, 0, 7) // A week from now
6now.AddDate(0, 0, -3) // Three days ago

For months and years, use date.AddMonths and date.AddYears. When the new month is too short for the day, they stop at its last day, so a monthly subscription that started on January 31 renews on February 28. AddDate would overflow into March instead:

1renewsAt := date.AddMonths(subscription.StartedAt, 1)
2anniversary := date.AddYears(user.CreatedAt, 1)
1renewsAt := date.AddMonths(subscription.StartedAt, 1)
2anniversary := date.AddYears(user.CreatedAt, 1)

To move a time to the start or end of a period, such as to count the orders placed today, use these functions. Each keeps the time's location, and the time's Truncate method rounds it down to a unit:

1date.StartOfDay(now) // October 5, 00:00:00
2date.EndOfDay(now) // October 5, 23:59:59.999999999
3date.StartOfWeek(now) // Monday, October 5, 00:00:00
4date.FirstOfMonth(now) // October 1, 00:00:00
5date.LastOfMonth(now) // October 31, 23:59:59.999999999
6now.Truncate(time.Minute) // October 5, 14:30:00
1date.StartOfDay(now) // October 5, 00:00:00
2date.EndOfDay(now) // October 5, 23:59:59.999999999
3date.StartOfWeek(now) // Monday, October 5, 00:00:00
4date.FirstOfMonth(now) // October 1, 00:00:00
5date.LastOfMonth(now) // October 31, 23:59:59.999999999
6now.Truncate(time.Minute) // October 5, 14:30:00

Comparing dates

To check whether a date has passed, use date.IsPast, or date.IsFuture for one that hasn't come yet. Both compare with date.Now, so they take the context. This handler sends users to the billing page once their 30-day trial is over:

1func Show(r *web.Request) *web.Response {
2 user := r.User[models.User]()
3 if date.IsPast(r, user.CreatedAt.AddDate(0, 0, 30)) {
4 return web.Redirect("/billing")
5 }
6 
7 return web.View("dashboard", user)
8}
1func Show(r *web.Request) *web.Response {
2 user := r.User[models.User]()
3 if date.IsPast(r, user.CreatedAt.AddDate(0, 0, 30)) {
4 return web.Redirect("/billing")
5 }
6 
7 return web.View("dashboard", user)
8}

The other checks are date.IsToday, date.IsTomorrow, date.IsYesterday, date.IsWeekend, and date.IsWeekday. They use the time's own location. Just after midnight UTC, it's still yesterday in Chicago, so convert the time before you ask about a user's day:

1date.IsToday(r, user.CreatedAt.In(chicago))
1date.IsToday(r, user.CreatedAt.In(chicago))

To compare two times, use their Before, After, and Equal methods, or date.IsSameDay and date.IsBetween (date.IsBetween includes both ends). Compare times with Equal rather than ==, which also compares their locations:

1startsAt := date.Now(r)
2endsAt := startsAt.AddDate(0, 0, 7)
3 
4if invoice.PaidAt.Before(invoice.DueAt) {
5 // Paid before the due date...
6}
7 
8if date.IsSameDay(event.StartsAt, event.EndsAt) {
9 // Single-day event...
10}
11 
12if date.IsBetween(date.Now(r), startsAt, endsAt) {
13 // Promotion is currently active...
14}
1startsAt := date.Now(r)
2endsAt := startsAt.AddDate(0, 0, 7)
3 
4if invoice.PaidAt.Before(invoice.DueAt) {
5 // Paid before the due date...
6}
7 
8if date.IsSameDay(event.StartsAt, event.EndsAt) {
9 // Single-day event...
10}
11 
12if date.IsBetween(date.Now(r), startsAt, endsAt) {
13 // Promotion is currently active...
14}

The time between dates

To show how many days a trial has left, count the whole days until it ends with date.DiffInDays, or the whole months with date.DiffInMonths. The result is negative when the second time is earlier. Both follow the calendar rather than counting hours, so a 23-hour day across a daylight saving change still counts as one day:

1trialEndsAt := user.CreatedAt.AddDate(0, 0, 30)
2 
3daysLeft := date.DiffInDays(date.Now(r), trialEndsAt)
1trialEndsAt := user.CreatedAt.AddDate(0, 0, 30)
2 
3daysLeft := date.DiffInDays(date.Now(r), trialEndsAt)

For hours, minutes, and seconds, subtract the times with Sub, which returns a time.Duration:

1elapsed := finishedAt.Sub(startedAt)
2 
3elapsed.Hours() // 2.5
4elapsed.Minutes() // 150
1elapsed := finishedAt.Sub(startedAt)
2 
3elapsed.Hours() // 2.5
4elapsed.Minutes() // 150

date.ForHumans formats a time as a human-readable relative string, in its largest whole unit:

1date.ForHumans(r, user.CreatedAt) // 3 minutes ago
2date.ForHumans(r, user.CreatedAt.AddDate(0, 0, 30)) // in 4 weeks
1date.ForHumans(r, user.CreatedAt) // 3 minutes ago
2date.ForHumans(r, user.CreatedAt.AddDate(0, 0, 30)) // in 4 weeks

In a table or activity feed, use date.CompactForHumans to shorten the unit while keeping ago or in:

1date.CompactForHumans(r, user.CreatedAt) // 3m ago
1date.CompactForHumans(r, user.CreatedAt) // 3m ago

To describe a length of time instead, such as how long an outage lasted, pass a time.Duration to date.DurationForHumans:

1date.DurationForHumans(incident.ResolvedAt.Sub(incident.StartedAt)) // 4 minutes
1date.DurationForHumans(incident.ResolvedAt.Sub(incident.StartedAt)) // 4 minutes

Calendar dates

Sometimes you need a day on the calendar with no time of day, such as an invoice's due date or a user's birthday. A time.Time would give the date a time and a location, and converting it to a user's location could move November 20 to the evening of November 19. Use a date.Date instead, which holds only a year, a month, and a day:

1type Invoice struct {
2 ID int64 `json:"id"`
3 DueOn date.Date `json:"due_on"`
4 PaidOn *date.Date `json:"paid_on"`
5}
1type Invoice struct {
2 ID int64 `json:"id"`
3 DueOn date.Date `json:"due_on"`
4 PaidOn *date.Date `json:"paid_on"`
5}

To ask whether an invoice is overdue for a user, compare its date with today's date in the user's location. Compare dates with ==, Before, and After, and count the days between them with DaysUntil, which is negative when the other date is earlier:

1today := date.TodayIn(r, chicago)
2 
3overdue := invoice.DueOn.Before(today)
4dueToday := invoice.DueOn == today
5daysLeft := today.DaysUntil(invoice.DueOn)
1today := date.TodayIn(r, chicago)
2 
3overdue := invoice.DueOn.Before(today)
4dueToday := invoice.DueOn == today
5daysLeft := today.DaysUntil(invoice.DueOn)

To move a date, call its AddDays, AddMonths, or AddYears method. AddMonths stops at the end of a short month, as date.AddMonths does for a time. To show a date, call Format with a layout, String for 2026-11-20, or ForHumans for a string such as in 3 days:

1invoice.DueOn.AddDays(-3) // 2026-11-17
2invoice.DueOn.Format("January 2, 2006") // November 20, 2026
3invoice.DueOn.ForHumans(r) // in 3 days
1invoice.DueOn.AddDays(-3) // 2026-11-17
2invoice.DueOn.Format("January 2, 2006") // November 20, 2026
3invoice.DueOn.ForHumans(r) // in 3 days

To get the date of a time, call date.Of(t.In(chicago)), which takes the date in the time's own location. To go the other way, such as to query the orders placed on a day, invoice.DueOn.In(chicago) returns midnight at the start of the date in Chicago.

Lengths of time

Whenever a Laravel function accepts a length of time, such as a cache entry's lifetime or a job's delay, pass a time.Duration like 10 * time.Minute. For example, this handler caches the user count for ten minutes:

1import (
2 "time"
3 
4 "go.laravel.com/cache"
5 "go.laravel.com/db"
6)
7 
8count, err := db.Table("users").Count(r)
9if err != nil {
10 return web.Fail(err)
11}
12 
13err = cache.Put(r, "users:count", count, 10*time.Minute)
1import (
2 "time"
3 
4 "go.laravel.com/cache"
5 "go.laravel.com/db"
6)
7 
8count, err := db.Table("users").Count(r)
9if err != nil {
10 return web.Fail(err)
11}
12 
13err = cache.Put(r, "users:count", count, 10*time.Minute)

Job delays and temporary signed URLs also accept an explicit target time through their Until forms, such as queue.DelayUntil and urls.TemporarySignedRouteUntil.

Configuration holds lengths of time as time.Duration values too, such as your session lifetime, which most applications set in minutes with SESSION_LIFETIME. To set one in code, change its field in your options' Configure function in bootstrap/app.go:

1func Options() laravel.Options {
2 return laravel.Options{
3 Configure: func(cfg *config.Config, env *config.Env) {
4 cfg.Session.Lifetime = 2 * time.Hour
5 },
6 // ...
7 }
8}
1func Options() laravel.Options {
2 return laravel.Options{
3 Configure: func(cfg *config.Config, env *config.Env) {
4 cfg.Session.Lifetime = 2 * time.Hour
5 },
6 // ...
7 }
8}

To learn more, check out the configuration guide's section on changing Laravel's configuration.

Parsing dates

When dates arrive as text from a form, an API, or a file, parse them with time.Parse and a layout. For a time written without a UTC offset, use time.ParseInLocation with the location the text was written in. For a calendar date, use date.Parse:

1publishedAt, err := time.Parse(time.RFC3339, "2026-10-05T19:30:00Z")
2meetingAt, err := time.ParseInLocation("01/02/2006 3:04 PM", "10/05/2026 2:30 PM", chicago)
3dueOn, err := date.Parse("2026-10-05")
1publishedAt, err := time.Parse(time.RFC3339, "2026-10-05T19:30:00Z")
2meetingAt, err := time.ParseInLocation("01/02/2006 3:04 PM", "10/05/2026 2:30 PM", chicago)
3dueOn, err := date.Parse("2026-10-05")

To build a time from its calendar parts, such as a meeting scheduled for 2:30 PM in Chicago, use time.Date:

1meeting := time.Date(2026, time.October, 5, 14, 30, 0, 0, chicago)
1meeting := time.Date(2026, time.October, 5, 14, 30, 0, 0, chicago)

Request input binds directly into these types: a time.Time field reads a timestamp with a UTC offset, such as 2026-10-05T19:30:00Z, and a date.Date field reads a date such as 2026-10-05. In a validated form, the date rules check a field's text: Date for a calendar date, DateTime for a timestamp, and DateFormat for text in a layout of your own, which you then parse with time.ParseInLocation.

Storing dates

Store moments such as a post's created_at as time.Time, and convert them to a user's location when you show them. A time.Time field maps to the columns created by Timestamps() and SoftDeletes(), in UTC, and a date.Date field maps to date columns. A column that may be NULL, such as published_at, is a pointer, which reads NULL as nil:

1type Post struct {
2 ID int64 `json:"id"`
3 Title string `json:"title"`
4 PublishedOn *date.Date `json:"published_on"`
5 CreatedAt time.Time `json:"created_at"`
6}
7 
8recent, err := model.Query[models.Post]().
9 Where("created_at", ">", date.Now(r).AddDate(0, 0, -7)).
10 Get(r)
1type Post struct {
2 ID int64 `json:"id"`
3 Title string `json:"title"`
4 PublishedOn *date.Date `json:"published_on"`
5 CreatedAt time.Time `json:"created_at"`
6}
7 
8recent, err := model.Query[models.Post]().
9 Where("created_at", ">", date.Now(r).AddDate(0, 0, -7)).
10 Get(r)

By default, Laravel stores times with microsecond precision and truncates outgoing times to that precision before writing them to the database. You may change this with DB_TIME_PRECISION. To learn more, check out the database guide's section on time precision.

A time.Time encodes to JSON as RFC 3339 text, such as "2026-10-05T19:30:00Z", so it round-trips through JSON and Inertia props. To learn how each database stores dates and times, check out the database guide's section on dates, times, and decimals.

Testing

To test code that depends on the time, such as the trial in Comparing dates, move the application's clock forward with the test client's Travel method instead of waiting:

1package feature_test
2 
3import (
4 "testing"
5 "time"
6 
7 "app/database/factories"
8 "app/tests"
9)
10 
11func TestTheDashboardRedirectsToBillingAfterTheTrial(t *testing.T) {
12 client := tests.New(t)
13 user := client.Create(factories.User)
14 
15 client.Travel(31 * 24 * time.Hour)
16 
17 client.ActingAs(&user).Get("/dashboard").AssertRedirect("/billing")
18}
1package feature_test
2 
3import (
4 "testing"
5 "time"
6 
7 "app/database/factories"
8 "app/tests"
9)
10 
11func TestTheDashboardRedirectsToBillingAfterTheTrial(t *testing.T) {
12 client := tests.New(t)
13 user := client.Create(factories.User)
14 
15 client.Travel(31 * 24 * time.Hour)
16 
17 client.ActingAs(&user).Get("/dashboard").AssertRedirect("/billing")
18}

For the other clock methods, such as TravelTo and Freeze, and details on how the time stands still after it travels, check out the testing guide's section on time travel.