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

Time Interval Helpers in Laravel 12.40

Tech Verse Daily

Tech Verse Daily

4 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.

    Latest Posts

    View All

    React 19: What’s new in React 19

    React 19: What’s new in React 19

    Laravel Strict Validation: Enforcing Exact PHP Types

    Laravel Strict Validation: Enforcing Exact PHP Types

    Next.js 16.0.1: The Essential Update Developers Shouldn’t Skip

    Next.js 16.0.1: The Essential Update Developers Shouldn’t Skip

    Time Interval Helpers in Laravel 12.40

    Time Interval Helpers in Laravel 12.40

    From GitHub Actions to Production Rollout: CI/CD for Laravel

    From GitHub Actions to Production Rollout: CI/CD for Laravel

    Top React Libraries and Frameworks Every Frontend Developer Should Know

    Top React Libraries and Frameworks Every Frontend Developer Should Know

    PHP 8.5 New Features and Deprecations

    PHP 8.5 New Features and Deprecations

    Manage, Track and Monitor Queue Jobs in Laravel with Vantage

    Manage, Track and Monitor Queue Jobs in Laravel with Vantage

    Tinkerwell 5: Introducing Tinkerwell Intelligence

    Tinkerwell 5: Introducing Tinkerwell Intelligence

    Roach PHP - Complete Web Scraping toolkit for PHP

    Roach PHP - Complete Web Scraping toolkit for PHP