ahmadajah03 Sat Nov 2020 1 year ago

Contoh Laravel Carbon Menambahkan Hari Hingga Saat Ini

Halo semua,

Dalam posting ini, kita akan belajar menambahkan hari carbon laravel. Kami akan melihat contoh menambahkan hari carbon laravel. Jika Anda memiliki pertanyaan tentang menambahkan hari ke tanggal di laravel maka saya akan memberikan contoh sederhana dengan solusi. Artikel ini akan memberi Anda contoh sederhana tentang menambahkan satu hari ke tanggal di laravel.

Anda dapat menambahkan hari pada tanggal saat ini menggunakan carbon di versi laravel 6, laravel 7 dan laravel 8.

Jika Anda perlu menambahkan hari atau lebih hari dalam tanggal maka Anda dapat menggunakan carbon dalam laravel. carbon menyediakan metode addDay() dan addDays() untuk menambahkan hari pada objek tanggal carbon. jadi mari kita lihat beberapa contoh untuk menambahkan hari dan hari dan sub hari dan hari dari tanggal.

Mari kita lihat contoh:

Contoh 1: Tambahkan Hari

<?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()->addDay();
             
        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-06 04:29:35.435474
    [timezone_type] => 3
    [timezone] => UTC
)

Contoh 2: Tambahkan Hari

<?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()->addDays(5);
             
        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-10 04:29:35.435474
    [timezone_type] => 3
    [timezone] => UTC
)

Contoh 3: Sub Day

<?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()->subDay();
   
        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-04 04:32:50.651151
    [timezone_type] => 3
    [timezone] => UTC
)

Contoh 4: Sub Days

<?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()->subDays(5);
  
        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-10-31 04:29:51.651673
    [timezone_type] => 3
    [timezone] => UTC
)

Saya harap ini dapat membantu Anda...

laravel laravel carbon