Laravel Carbon Subtract Seconds Example Tutorial

Aug 18, 2021 . Admin



Hi Guys,

Today, In this article, I will explain to you how to subtract seconds in laravel using carbon in laravel so it can easy to use in laravel app.

So, if you require to subtract one second or more seconds you can use carbon in laravel. carbon provide inbuilt function subSecond()

and subSeconds() method to subtract seconds on carbon date object. so let's see some examples to subtract second and seconds.

Here, I will give you a full example of how to subtract seconds in laravel using carbon in laravel. so follow my example code and you can use your laravel application to copy that code.

Example : 1
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon subtract second...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subSecond();

        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-18 04:16:26.251155

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-18 04:16:25.251157

    [timezone_type] => 3

    [timezone] => UTC
)
Example : 2
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon subtract seconds...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subSeconds(30);

        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-18 04:17:28.680348

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-18 04:16:58.680351

    [timezone_type] => 3
    
    [timezone] => UTC
)

It Will Help You..

#PHP #Laravel 8 #Laravel