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