Dates and Time
- Introduction
- The current time
- Formatting dates
- Date arithmetic
- Lengths of time
- Parsing dates
- Storing dates
- Testing
Introduction
Web applications constantly work with dates and timestamps, from displaying when a post was published to expiring invitations and trials. Laravel re-exports the Jiff date and time library as laravel::time, so you do not need to add a separate dependency. Jiff handles timezone conversions, daylight saving transitions, and calendar-aware arithmetic (so adding one month to January 31 lands on the end of February).
Most application code works with three types:
| Type | Use it for |
|---|---|
ZonedDateTime |
A moment paired with a timezone, used for formatting and calendar calculations (laravel::now() returns one). |
Timestamp |
A timezone-independent instant, ideal for database storage and comparisons. |
civil::Date |
A calendar date without a time of day, such as a birthday. |
The Dates trait in laravel::time adds convenience helpers such as is_past, plus, minus, diff_in_days, and format to all three types, and is included in the prelude.
The current time
laravel::now() returns the current time as a ZonedDateTime in UTC. You may call it anywhere in your application, such as in a handler or a job:
1let now = laravel::now();23now.year(); // 20264now.date(); // 2026-10-055now.weekday(); // Monday1let now = laravel::now();23now.year(); // 20264now.date(); // 2026-10-055now.weekday(); // Monday
Always read the time with laravel::now(), not Jiff's ZonedDateTime::now() or the standard library's SystemTime::now(), so your tests can move the clock. For today's date without a time, call laravel::time::today(), which returns a civil::Date in UTC.
Timezones
Laravel has no application-wide timezone setting. Its clock, time travel in tests, and scheduled tasks all use UTC. Keep your dates in UTC while you calculate and store them, and convert them with in_tz when you show a user their local time:
1let chicago = laravel::now().in_tz("America/Chicago")?;1let chicago = laravel::now().in_tz("America/Chicago")?;
Jiff includes the IANA time zone database, so names such as America/Chicago work even on a server that doesn't have one installed.
Formatting dates
To show a date, call format with strftime's % directives:
1use laravel::time::Dates;23let now = laravel::now();45now.format("%B %-d, %Y"); // October 5, 20266now.format("%b %-d, %H:%M"); // Oct 5, 14:307now.format("%A, %-I:%M %p"); // Monday, 2:30 PM8now.format("%Y-%m-%d"); // 2026-10-051use laravel::time::Dates;23let now = laravel::now();45now.format("%B %-d, %Y"); // October 5, 20266now.format("%b %-d, %H:%M"); // Oct 5, 14:307now.format("%A, %-I:%M %p"); // Monday, 2:30 PM8now.format("%Y-%m-%d"); // 2026-10-05
Jiff's documentation lists every directive. format panics on an invalid format, so cover the formats you write with a test. A Timestamp formats in UTC, so convert it with in_tz first to show a user's local time.
Dates in views
In templates, the date filter formats a date with the same directives, and the for_humans filter formats a relative time string matching for_humans in Rust. Without a format, date renders dates like October 5, 2026. Dates display in UTC unless you pass a tz:
1<time datetime="{{ post.created_at }}">{{ post.created_at|date }}</time> {# October 5, 2026 #}2{{ post.created_at|date("%b %-d, %H:%M") }} {# Oct 5, 14:30 #}3{{ post.created_at|date(tz="America/Chicago") }} {# October 5, 2026 #}4{{ post.created_at|for_humans }} {# 3 minutes ago #}1<time datetime="{{ post.created_at }}">{{ post.created_at|date }}</time> {# October 5, 2026 #}2{{ post.created_at|date("%b %-d, %H:%M") }} {# Oct 5, 14:30 #}3{{ post.created_at|date(tz="America/Chicago") }} {# October 5, 2026 #}4{{ post.created_at|for_humans }} {# 3 minutes ago #}
Date arithmetic
To find the date a week from now or three days ago, call plus or minus with a span. Each returns a new date and leaves the original as it was. ToSpan, which the prelude also imports, builds spans from integers in any unit:
1use laravel::time::{Dates, ToSpan};23let now = laravel::now();45now.plus(1.week());6now.minus(3.days());7now.plus(2.hours().minutes(30));8now.plus(1.month()); // A month after January 31 is February 281use laravel::time::{Dates, ToSpan};23let now = laravel::now();45now.plus(1.week());6now.minus(3.days());7now.plus(2.hours().minutes(30));8now.plus(1.month()); // A month after January 31 is February 28
Days, weeks, and months follow the date's timezone, so adding a day across a daylight saving change preserves the same time of day. For spans from user input, use Jiff's checked_add and checked_sub instead, which return an error rather than panicking when the computed date falls outside the years -9999 to 9999.
To move a date to the start or end of a period, such as to count the orders placed today, use these Jiff methods. Each returns a Result:
1use laravel::time::Unit;23now.start_of_day()?;4now.end_of_day()?;5now.first_of_month()?;6now.last_of_month()?;7now.round(Unit::Minute)?;1use laravel::time::Unit;23now.start_of_day()?;4now.end_of_day()?;5now.first_of_month()?;6now.last_of_month()?;7now.round(Unit::Minute)?;
Comparing dates
To check whether a date has passed, use is_past, or is_future for one that hasn't come yet. Both compare with laravel::now(). This handler sends users to the billing page once their 30-day trial is over:
1use laravel::prelude::*;23pub async fn show(user: Authenticated<User>) -> laravel::Result<Response> {4 if user.created_at.plus(30.days()).is_past() {5 return Ok(Redirect::to("/billing").into_response());6 }78 Ok(view("dashboard", data! { name => &user.name }).into_response())9}1use laravel::prelude::*;23pub async fn show(user: Authenticated<User>) -> laravel::Result<Response> {4 if user.created_at.plus(30.days()).is_past() {5 return Ok(Redirect::to("/billing").into_response());6 }78 Ok(view("dashboard", data! { name => &user.name }).into_response())9}
The other checks are is_today, is_tomorrow, is_yesterday, is_weekend, and is_weekday. They use a ZonedDateTime's own timezone, and UTC for a Timestamp. Just after midnight UTC, it's still yesterday in Chicago, so convert the date before you ask about a user's day:
1user.created_at.in_tz("America/Chicago")?.is_today();1user.created_at.in_tz("America/Chicago")?.is_today();
To compare two dates, use operators such as < and ==, or is_same_day and is_between (is_between is inclusive of both endpoints):
1let starts_at = laravel::now();2let ends_at = starts_at.plus(7.days());34if invoice.paid_at < invoice.due_at {5 // Paid before the due date...6}78if event.starts_at.is_same_day(&event.ends_at) {9 // Single-day event...10}1112if laravel::now().is_between(&starts_at, &ends_at) {13 // Promotion is currently active...14}1let starts_at = laravel::now();2let ends_at = starts_at.plus(7.days());34if invoice.paid_at < invoice.due_at {5 // Paid before the due date...6}78if event.starts_at.is_same_day(&event.ends_at) {9 // Single-day event...10}1112if laravel::now().is_between(&starts_at, &ends_at) {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 diff_in_days. diff_in_hours, diff_in_minutes, and diff_in_seconds count the other units. The result is negative when the other date is earlier:
1let trial_ends_at = user.created_at.plus(30.days());23let days_left = laravel::now().timestamp().diff_in_days(&trial_ends_at);1let trial_ends_at = user.created_at.plus(30.days());23let days_left = laravel::now().timestamp().diff_in_days(&trial_ends_at);
For a span broken across several units, such as 2 hours and 30 minutes, use Jiff's until and since. Pass a (Unit, &date) tuple when you want to specify the largest unit in the span:
1use laravel::time::Unit;23let elapsed = finished_at.since(&started_at)?;45elapsed.get_hours(); // 26elapsed.get_minutes(); // 3078let remaining = laravel::now().until((Unit::Day, &deadline))?;910remaining.get_days(); // 411remaining.get_hours(); // 61use laravel::time::Unit;23let elapsed = finished_at.since(&started_at)?;45elapsed.get_hours(); // 26elapsed.get_minutes(); // 3078let remaining = laravel::now().until((Unit::Day, &deadline))?;910remaining.get_days(); // 411remaining.get_hours(); // 6
for_humans formats a date as a human-readable relative string:
1user.created_at.for_humans(); // 3 minutes ago2user.created_at.plus(30.days()).for_humans(); // in 4 weeks3laravel::time::today().for_humans(); // today1user.created_at.for_humans(); // 3 minutes ago2user.created_at.plus(30.days()).for_humans(); // in 4 weeks3laravel::time::today().for_humans(); // today
In a table or activity feed, use compact_for_humans to shorten the unit while keeping ago or in:
1user.created_at.compact_for_humans(); // 3m ago1user.created_at.compact_for_humans(); // 3m ago
Lengths of time
Whenever a Laravel API accepts a duration, such as a cache TTL or a job delay, pass a span like 10.minutes(). For example, this caches the user count for ten minutes:
1use laravel::cache;2use laravel::time::ToSpan;34let count = User::query().count().await?;56cache::put("users:count", count, 10.minutes()).await?;1use laravel::cache;2use laravel::time::ToSpan;34let count = User::query().count().await?;56cache::put("users:count", count, 10.minutes()).await?;
Job delays and temporary URL expirations also accept an explicit target date such as laravel::now().end_of_day()?.
When Laravel stores or exposes a configured duration (such as your session lifetime), it uses ExactDuration. You may compare it directly against a span (such as lifetime > 1.hour()), and it dereferences to std::time::Duration for standard library or third-party APIs. To assign one in configuration, call .into() on a span in src/app.rs:
1Application::configure()2 .with_config(|config, _| {3 config.session.lifetime = 2.hours().into();4 })1Application::configure()2 .with_config(|config, _| {3 config.session.lifetime = 2.hours().into();4 })
Parsing dates
When dates arrive as strings from a form, API, or file, call parse with the target type:
1use laravel::time::{Timestamp, ZonedDateTime, civil};23let timestamp: Timestamp = "2026-10-05T19:30:00Z".parse()?;4let zoned: ZonedDateTime = "2026-10-05T14:30:00-05:00[America/Chicago]".parse()?;5let date: civil::Date = "2026-10-05".parse()?;1use laravel::time::{Timestamp, ZonedDateTime, civil};23let timestamp: Timestamp = "2026-10-05T19:30:00Z".parse()?;4let zoned: ZonedDateTime = "2026-10-05T14:30:00-05:00[America/Chicago]".parse()?;5let date: civil::Date = "2026-10-05".parse()?;
To construct a date from individual calendar components, such as a meeting scheduled for 2:30 PM in Chicago, start from civil::date:
1let meeting = civil::date(2026, 10, 5).at(14, 30, 0, 0).in_tz("America/Chicago")?;1let meeting = civil::date(2026, 10, 5).at(14, 30, 0, 0).in_tz("America/Chicago")?;
Request input deserializes directly into these types. In a validated form, a civil::Date field validates calendar dates and a Timestamp field validates timestamps with a UTC offset. For custom formats, apply date_format to a String field and parse it with civil::Date::strptime("%d/%m/%Y", &input)?.
Storing dates
Store instants such as a post's created_at as Timestamp, and convert them to ZonedDateTime when formatting for a user's local timezone. Timestamp maps to the columns created by timestamps() and soft_deletes(), while civil::Date maps to date columns:
1use laravel::db;2use laravel::time::{Timestamp, civil::Date};34#[derive(db::FromRow)]5pub struct Post {6 pub title: String,7 pub published_on: Option<Date>,8 pub created_at: Timestamp,9}1011let recent: Vec<Post> = db::table("posts")12 .filter(db::col("created_at").gt(laravel::now().minus(7.days()).timestamp()))13 .get()14 .await?;1use laravel::db;2use laravel::time::{Timestamp, civil::Date};34#[derive(db::FromRow)]5pub struct Post {6 pub title: String,7 pub published_on: Option<Date>,8 pub created_at: Timestamp,9}1011let recent: Vec<Post> = db::table("posts")12 .filter(db::col("created_at").gt(laravel::now().minus(7.days()).timestamp()))13 .get()14 .await?;
By default, Laravel stores timestamps with microsecond precision (6 fractional digits) and truncates outgoing values to that precision before writing them to the database. You can customize this with DB_TIME_PRECISION; see time precision in the database guide for details.
Jiff's types support Serde. A Timestamp serializes 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 engine stores dates and timestamps, check out dates and timestamps in the database guide.
Testing
To test code that depends on the time, such as the trial in Comparing dates, move the application's clock forward with travel instead of waiting:
1use app::models::User;2use laravel::model::create;3use laravel::testing::prelude::*;45#[laravel::test]6async fn the_dashboard_redirects_to_billing_after_the_trial(7 app: Application,8) -> laravel::Result<()> {9 let user = create!(User {10 name: "Taylor Otwell",12 password: "password",13 })14 .await?;1516 app.travel(31.days());1718 app.get("/dashboard").acting_as(&user).await.assert_redirect_to("/billing");1920 Ok(())21}1use app::models::User;2use laravel::model::create;3use laravel::testing::prelude::*;45#[laravel::test]6async fn the_dashboard_redirects_to_billing_after_the_trial(7 app: Application,8) -> laravel::Result<()> {9 let user = create!(User {10 name: "Taylor Otwell",12 password: "password",13 })14 .await?;1516 app.travel(31.days());1718 app.get("/dashboard").acting_as(&user).await.assert_redirect_to("/billing");1920 Ok(())21}
For additional clock helpers and details on freezing time between calls, check out time travel in the testing guide.