---
description: >-
  https://www.itsolutionstuff.com/post/laravel-carbon-diffforhumans-exampleexample.html
---

# Laravel Carbon, diffForHumans, firstOfMonth, endOfMonth, Compare Two Dates, fomat full (ok)

Các bài toán :thumbsup::thumbsup:

> Laravel Carbon diffForHumans() Example
>
> Laravel Carbon createFromFormat() Example
>
> Laravel Carbon Check Date is in Past Date Code
>
> Carbon Get First Day of Specific Month Example
>
> Laravel Carbon Get Last Day of Month Example\
> Laravel Carbon Convert String to Date Example
>
> Laravel Carbon Get Year from Date Example
>
> Laravel Carbon Get Day from Date Example
>
> How to Get Month Name from Date in Laravel?
>
> How to Compare Two Dates in Laravel Carbon?
>
> Laravel Carbon Get Previous Month Example
>
> Laravel Carbon Get Next Month Example\
> Laravel Carbon Get All Months Between Two Dates Example
>
> Laravel Carbon Check If Date is Greater Than Other Date
>
> Laravel Carbon Get Tomorrow Date Example
>
> Laravel Carbon Get Yesterday Date Example
>
> Laravel Carbon Get All Dates Between Two Dates Example
>
> Laravel Carbon Check Current Time Between Two Date Time Example\
> Laravel Carbon Check Current Date Between Two Dates Example
>
> Laravel Carbon Get Current Date Time Example
>
> Laravel Carbon Subtract Seconds Example
>
> Laravel Carbon addSeconds() | Laravel Carbon Add Seconds Example
>
> Laravel Carbon Subtract Minutes Example
>
> Laravel Carbon addMinutes() | Laravel Carbon Add Minutes Example
>
> Laravel Carbon Subtract Hours Example
>
> Laravel Carbon addHours() | Laravel Carbon Add Hours Example
>
> Laravel Carbon Subtract Year Example
>
> Laravel Carbon addYears() | Laravel Carbon Add Year Example
>
> Laravel Carbon Subtract Months from Date Example
>
> Laravel Carbon addMonths() | Laravel Carbon Add Months Example
>
> Laravel Carbon Subtract Days to Date Example
>
> Laravel Carbon Add Days to Date Example
>
> Laravel age calculate from date using carbon
>
> Laravel - How to Get Difference of Time in Minutes?
>
> How to get year, month and day from timestamp date using carbon in Laravel?
>
> Laravel Change Date Format using Carbon Example

C:\xampp\htdocs\test\routes\web.php

```
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SignaturePadController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/', [SignaturePadController::class, 'index']);
```

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate = '12/08/2020';
    $result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans();
    echo '<pre>';
      var_export($result);
    echo '</pre>';
  }
}
```

### Kết quả: 1 year ago

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate  = '12/08/2020';
    $myDate2 = '12/10/2020';
    $newDate = Carbon::createFromFormat('m/d/Y', $myDate2);
    $result  = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans($newDate);
    echo '<pre>';
    var_export($result);
    echo '</pre>';
  }
}
```

### Kết quả: 2 days before

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate = '12/08/2020';
    $result = Carbon::createFromFormat('m/d/Y', $myDate)->format('Y-m-d');
    echo '<pre>';
    var_export($result);
    echo '</pre>';
  }
}
```

### Kết quả: convert format '12/08/2020' to '12-08-2020'

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate = '05/08/2022';
    $result = Carbon::createFromFormat('m/d/Y', $myDate)->isPast();
    echo '<pre>';
    var_export($result);
    echo '</pre>';
  }
}
```

> Ví dụ hôm nay là tháng 10 ngày 5 năng 2020 giờ kiểm tra tháng 10 ngày 4 năng 2020 thì nó đã trôi qua rồi kết quả nhận được là đúng 😁

### Kiểm tra ngày đã trôi qua chưa Kết quả: boolean 😒

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate = '12/08/2020';
    $date = Carbon::createFromFormat('m/d/Y', $myDate)->firstOfMonth()->format('Y-m-d');
    echo '<pre>';
    var_export($date);
    echo '</pre>';
  }
}
Tương tự 😁😁😁
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $myDate = '12/08/2020';
    $date = Carbon::createFromFormat('m/d/Y', $myDate)->endOfMonth()->format('Y-m-d');
    echo '<pre>';
    var_export($date);
    echo '</pre>';
  }
}
```

### Kết quả: 2020-12-01 tương tự 2020-12-31

```
eq() equals
ne() not equals
gt() greater than
gte() greater than or equals
lt() less than
lte() less than or equals
```

C:\xampp\htdocs\test\app\Http\Controllers\SignaturePadController.php

```
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class SignaturePadController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '12/01/2020 10:20:00');
    $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '12/01/2020 10:20:00');
    $result = $date1->eq($date2);
    var_dump($result);
  }
}
```
