andriajah Tue Jul 2023 9 months ago

Laravel Carbon addHours() | Contoh Laravel Carbon Add Hours

Saya akan menunjukkan contoh laravel carbon add hour. jika Anda memiliki pertanyaan tentang laravel carbon add hours maka saya akan memberikan contoh sederhana dengan solusinya. saya jelaskan secara sederhana langkah demi langkah karbon laravel tambahkan 1 jam. saya ingin menunjukkan kepada Anda laravel carbon addHours().

Anda dapat menambahkan jam pada tanggal saat ini menggunakan karbon di versi laravel 6, laravel 7, laravel 8, laravel 9 dan laravel 10.

Jika Anda perlu menambahkan jam atau lebih pada tanggal maka Anda dapat menggunakan karbon di laravel. karbon menyediakan metode addHour() dan addHours() untuk menambahkan jam pada objek tanggal karbon. jadi mari kita lihat beberapa contoh untuk menambahkan jam dan jam dan sub jam dan tahun dari tanggal.

Mari kita lihat contoh:

Contoh 1: Tambahkan Jam

<?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()->addHour();
             
        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 05:29:35.435461
    [timezone_type] => 3
    [timezone] => UTC
)

Contoh 2: Tambahkan Jam

<?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()->addHours(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-05 09:29:35.435461
    [timezone_type] => 3
    [timezone] => UTC
)

Contoh 3: Sub Jam

<?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()->subHour();
   
        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 03:32:50.651145
    [timezone_type] => 3
    [timezone] => UTC
)

 

Contoh 4: Sub Jam

<?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()->subHours(2);
  
        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 02:29:51.651667
    [timezone_type] => 3
    [timezone] => UTC
)

Saya harap ini dapat membantu Anda ...

laravel. laravel 10 carbon laravel tutorial