PHP Carbon Usage Guide with Examples

Carbon is a PHP library for handling dates and times, designed to streamline operations and increase development efficiency. It offers a user-friendly API with various helpful features including date comparison, formatting, and modification.

Here are some common usage examples of the Carbon library:

  1. Create an instance of Carbon:
$date = Carbon::now();
  1. Format date:
echo $date->format('Y-m-d H:i:s');
  1. Add or subtract time intervals.
$date->addDays(7);
$date->subMonths(1);
  1. Compare dates:
if ($date1->gt($date2)) {
    echo 'Date 1 is greater than Date 2';
}
  1. Analyze the date string.
$date = Carbon::parse('2022-01-01');
  1. Get different parts of the date.
echo $date->year;
echo $date->month;
echo $date->day;

These are just some usage examples of the Carbon library. You can refer to the official documentation of Carbon for more detailed information and usage examples.

bannerAds