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

    Introducing the Laravel AI SDK — Build Smarter Apps with AI

    Introducing the Laravel AI SDK — Build Smarter Apps with AI

    Laravel AI SDK: Building AI-Powered Applications the Laravel Way

    Laravel AI SDK: Building AI-Powered Applications the Laravel Way

    Getting Started with Mago – The Fastest PHP Tooling Chain

    Getting Started with Mago – The Fastest PHP Tooling Chain

    Best Stack Recommendations for Laravel Projects (Battle-Tested in Production)

    Best Stack Recommendations for Laravel Projects (Battle-Tested in Production)

    Laravel + React Authentication the Right Way: Sanctum, JWT, or Passport?

    Laravel + React Authentication the Right Way: Sanctum, JWT, or Passport?

    Laravel PDF Generator: Spatie Laravel PDF vs Laravel DomPDF (In-Depth Comparison)

    Laravel PDF Generator: Spatie Laravel PDF vs Laravel DomPDF (In-Depth Comparison)

    how to systematically optimize Laravel databases in production

    how to systematically optimize Laravel databases in production

    Optimize Images in Laravel with Intervention Image

    Optimize Images in Laravel with Intervention Image

    Common Security Mistakes in Laravel Apps and How to Fix Them Properly

    Common Security Mistakes in Laravel Apps and How to Fix Them Properly

    Clean, Reusable Query Logic the Right Way: Laravel Global Scopes & Local Scopes

    Clean, Reusable Query Logic the Right Way: Laravel Global Scopes & Local Scopes