ItSolutionStuff.com

Laravel Carbon addSeconds() | Laravel Carbon Add Seconds Example

By Hardik Savani β€’ April 16, 2024
Laravel

Hi,

In this tutorial, i will show you laravel carbon add seconds. step by step explain laravel carbon add second. We will use laravel carbon addSecond(). i would like to share with you laravel carbon addSeconds(). Alright, let’s dive into the steps.

You can add seconds on current date time using carbon in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

If you need to add second or more seconds in date then you can use carbon in laravel. carbon provide addSecond() and addSeconds() method to add seconds on carbon date object. so let's see some examples to adding second and seconds and sub second and seconds from date.

Let's see example:

Example 1: Add Second

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addSecond();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:36.435461

[timezone_type] => 3

[timezone] => UTC

)

Example 2: Add Seconds

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addSeconds(20);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:55.435461

[timezone_type] => 3

[timezone] => UTC

)

Example 3: Sub Second

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subSecond();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:32:50.651145

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:32:49.651145

[timezone_type] => 3

[timezone] => UTC

)

Example 4: Sub Seconds

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subSeconds(20);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:51.651667

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:31.651667

[timezone_type] => 3

[timezone] => UTC

)

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Carbon addMinutes() | Laravel Carbon Add Minutes Example

Read Now β†’
β˜…

Laravel Carbon Subtract Hours Example

Read Now β†’
β˜…

Laravel Carbon addHours() | Laravel Carbon Add Hours Example

Read Now β†’
β˜…

Laravel Carbon Subtract Year Example

Read Now β†’
β˜…

Laravel Carbon addYears() | Laravel Carbon Add Year Example

Read Now β†’
β˜…

Laravel Carbon Subtract Months from Date Example

Read Now β†’
β˜…

Laravel Carbon addMonths() | Laravel Carbon Add Months Example

Read Now β†’
β˜…

Laravel Carbon Subtract Days to Date Example

Read Now β†’
β˜…

Laravel Carbon Add Days to Date Example

Read Now β†’
β˜…

How to Get Hours Difference Between Two Dates in Laravel?

Read Now β†’