Laravel Carbon Add Seconds Example Tutorial


Share:
Published By: Admin, Published On: Aug 17, 2021, Category: PHP Laravel 8 Laravel


Hi All,

Today, In this example, i will share with you how to integrate seconds in laravel using carbon in laravel so it can easy to use in laravel app.

If you require to integrate one second or more seconds you can use carbon in laravel. carbon provide inbuilt function addSecond() and addSeconds() method to add seconds on carbon date object. so let's see some examples to add second and seconds.

Here, I will give you a full example of how to add 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 add second...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addSecond();

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

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-17 04:27:47.745777

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-17 04:27:48.745783

    [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 add seconds...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addSeconds(2);

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

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-17 04:29:21.704911

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-17 04:29:23.704919

    [timezone_type] => 3

    [timezone] => UTC
)

It Will Help You..

Share: