Tech Verse Logo
Enable dark mode
Time Interval Helpers in Laravel 12.40

Time Interval Helpers in Laravel 12.40

Md. Mostafijur RahmanMMd. Mostafijur Rahman

Md. Mostafijur Rahman

1 min read

The Laravel team released version 12.40.0 this week with new time helper functions, the ability to schedule tasks to run on specific days, pausing and resuming queues, and more.

Time Helpers

Taylor Otwell added time helper functions to the Illuminate\Support namespace and new methods on the support Carbon class:

use Illuminate\Support\Carbon;
 
// Plus and minus methods support years, months, weeks,
// days, hours, minutes, seconds, microseconds.
Carbon::now()->plus(years: 1, days: 5);
 
Carbon::now()->minus(weeks: 4);

The support namespace has the following helpers, which return a CarbonInterval instance:

use function Illuminate\Support\{seconds, minutes, hours, days, years};
 
seconds(5);
minutes(5);
hours(1);
days(30);
years(1);
 
/*
Carbon\CarbonInterval {#1277
    interval: + 1y,
}
*/

Here are a few examples of how you might use the support Carbon methods and helper functions:

use function Illuminate\Support\minutes;
 
Cache::put('name', 'Taylor', minutes(5));
 
Invitation::create([
    'expires_at' => now()->plus(weeks: 1),
]);

Schedule Tasks on Specific Days

Yousef Kadah contributed a daysOfMonth() method to run tasks on multiple, specific days of the month:

// Variadic syntax
$schedule->command('generate-reports')
    ->daysOfMonth(1, 10, 20);
 
// Array syntax
$schedule->command('generate-reports')
    ->daysOfMonth([1, 10, 20]);

Encoding Validation Rule

Jamie York contributed an encoding validation rule that checks the contents of a file and ensures it matches a specific encoding:

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\File;
 
Validator::validate($input, [
    'attachment' => [
        'required',
        File::types(['csv'])->encoding('utf-8'),
    ],
]);

Pause/Resume a Queue

Yousef Kadah contributed the ability to pause and resume queues using a command. The queue argument is the connection and queue name. For example, the following is the database connection and default is the queue name:

php artisan queue:pause database:default
 
php artisan queue:continue database:default

See the documentation for details on pausing and resuming queue workers.

Md. Mostafijur RahmanMMd. Mostafijur Rahman

WRITTEN BY

Md. Mostafijur Rahman

    Latest Posts

    View All

    React Context: When to Reach for a Store

    React Context: When to Reach for a Store

    Custom React Hooks: Abstraction vs Indirection

    Custom React Hooks: Abstraction vs Indirection

    Stop Wasting React Performance on memo and useMemo

    Stop Wasting React Performance on memo and useMemo

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Stop Re-rendering: When to Drop useEffect in React 19

    Stop Re-rendering: When to Drop useEffect in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Generating Open Graph Images at the Edge in Next.js 16

    Generating Open Graph Images at the Edge in Next.js 16

    Fix Next.js Third Party Script Performance

    Fix Next.js Third Party Script Performance

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes