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

    Laravel 12.44: Adds HTTP Client afterResponse() Callbacks

    Laravel 12.44: Adds HTTP Client afterResponse() Callbacks

    Laravel Artifact: Manage Your Media Easily

    Laravel Artifact: Manage Your Media Easily

    Handling Large File Uploads in Laravel: A Guide to Chunking & Resuming

    Handling Large File Uploads in Laravel: A Guide to Chunking & Resuming

    Next-Gen Laravel Deployment: FrankenPHP + Octane on Ubuntu VPS

    Next-Gen Laravel Deployment: FrankenPHP + Octane on Ubuntu VPS

    Speed Up Your Laravel App: Mastering Concurrent API Requests with Http::pool and Batch

    Speed Up Your Laravel App: Mastering Concurrent API Requests with Http::pool and Batch

    Beyond the Basics: Building Production-Ready APIs with Laravel

    Beyond the Basics: Building Production-Ready APIs with Laravel

    PHP 8.6: Expected Release Window and RFCs to Watch

    PHP 8.6: Expected Release Window and RFCs to Watch

    Downloading Files from External URLs in Laravel

    Downloading Files from External URLs in Laravel

    Pause and Resume Laravel Queue Workers on Demand

    Pause and Resume Laravel Queue Workers on Demand

    Resume Canvas - Open Source Resume Builder

    Resume Canvas - Open Source Resume Builder