Using Carbon PHP Extension for Simplified Date and Time Manipulation
This article demonstrates how to install and use the Carbon PHP extension to simplify date and time operations, covering retrieval of current, today, yesterday, tomorrow, parsing specific strings, timezone handling, adding and subtracting years, months, weeks, days, hours, minutes, seconds, and formatting dates in various styles.
In everyday PHP development, handling dates and times can be cumbersome due to complex native functions. The Carbon extension provides a more readable and semantic way to work with date and time.
1. Install the Extension
composer require nesbot/carbon2. Use Carbon in Your Code
use Carbon\Carbon;3. Retrieve Dates and Times
// Get current time
$current = Carbon::now();
echo "Current time: " . $current . PHP_EOL;
// Get today (midnight)
$today = Carbon::today();
echo "Today: " . $today . PHP_EOL;
// Get yesterday
$yesterday = Carbon::yesterday();
echo "Yesterday: " . $yesterday . PHP_EOL;
// Get tomorrow
$tomorrow = Carbon::tomorrow();
echo "Tomorrow: " . $tomorrow . PHP_EOL;4. Parse Specific Strings
$newYear = new Carbon('first day of January 2023');
echo "Parsed date: " . $newYear . PHP_EOL;5. Set a Specific Timezone
$newYearPST = new Carbon('first day of January 2023', 'Asia/Shanghai');
echo "Timezone date: " . $newYearPST . PHP_EOL;6. Manipulate Dates
// Add 30 days
$trialExpires = Carbon::now()->addDays(30);
echo "Add 30 days: " . $trialExpires . PHP_EOL;
// Create a custom date
$dt = Carbon::create(2023, 1, 12, 12, 0, 0);
// Add years, months, weeks, days, hours, minutes, seconds
$dt = $dt->addYears(5);
$dt = $dt->addMonths(60);
$dt = $dt->addWeeks(3);
$dt = $dt->addDays(29);
$dt = $dt->addHours(24);
$dt = $dt->addMinutes(50);
$dt = $dt->addSeconds(50);
echo "Modified date: " . $dt . PHP_EOL;
// Subtract the same intervals
$dt = $dt->subYears(5);
$dt = $dt->subMonths(60);
$dt = $dt->subWeeks(3);
$dt = $dt->subDays(29);
$dt = $dt->subHours(24);
$dt = $dt->subMinutes(50);
$dt = $dt->subSeconds(50);
echo "Reverted date: " . $dt . PHP_EOL;7. Work with Weekdays (Skipping Weekends)
// Add 2 weekdays
$dt = $dt->addWeekdays(2);
// Add a single weekday
$dt = $dt->addWeekday();
// Subtract a weekday
$dt = $dt->subWeekday();
// Add 4 weekdays
$dt = $dt->addWeekdays(4);
// Subtract 4 weekdays
$dt = $dt->subWeekdays(4);8. Formatting Dates
$dt = Carbon::now();
// Date only
echo $dt->toDateString(); // e.g., 2023-01-12
// English formatted date
echo $dt->toFormattedDateString(); // e.g., Jan 12, 2023
// Time only
echo $dt->toTimeString(); // e.g., 13:05:21
// Full date and time
echo $dt->toDateTimeString(); // e.g., 2023-01-12 13:03:49
// English day, date and time
echo $dt->toDayDateTimeString(); // e.g., Thu, Jan 12, 2023 1:03 PMFor many more operations, refer to the official Carbon documentation at https://carbon.nesbot.com .
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.