Laravel Carbon Subtract Seconds Example

By Hardik Savani November 23, 2020 Category : Laravel

Now, let's see example of laravel carbon subtract seconds. i would like to show you laravel carbon subtract second. This post will give you simple example of laravel carbon subSecond(). this example will help you laravel carbon subSeconds(). You just need to some step to done sub seconds to date in laravel.

You can subtract seconds on current date time using carbon in laravel 6, laravel 7 and laravel 8 version.

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

Let's see example:

Example 1: 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 2: 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

)

Example 3: 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 4: 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

)

I hope it can help you...

Tags :