Engineering note · Laravel

Handling Dates with Carbon Date Helpers

Carbon Helper simplifies working with dates, making it easier to perform common tasks like formatting, comparing, and manipulating dates.

By Published

Laravel

Example 1: Getting the Current Date and Time

You can easily get the current date and time using Carbon.

CODE SAMPLE Code snippet
use Carbon\Carbon;

$currentDateTime = Carbon::now();
echo $currentDateTime; // e.g., 2024-10-27 15:30:00

Example 2: Formatting Dates

Format a date in a more human-readable way.

CODE SAMPLE Code snippet
$formattedDate = Carbon::now()->format('l, F j, Y');
echo $formattedDate; // e.g., Saturday, October 27, 2024

Example 3: Adding or Subtracting Time

You can manipulate dates by adding or subtracting time easily.

CODE SAMPLE Code snippet
$nextWeek = Carbon::now()->addWeek();
echo $nextWeek; // e.g., 2024-11-03 15:30:00

$lastMonth = Carbon::now()->subMonth();
echo $lastMonth; // e.g., 2024-09-27 15:30:00

Example 4: Check if a Date is in a Given Week

Check if a specific date falls within the current week.

CODE SAMPLE Code snippet
$date = Carbon::createFromDate(2024, 10, 25);

if ($date->isCurrentWeek()) {
    echo "$date is in the current week.";
} else {
    echo "$date is not in the current week.";
}

Example 5: Get the Next Business Day

Carbon can help you find the next business day, skipping weekends.

CODE SAMPLE Code snippet
$date = Carbon::createFromDate(2024, 10, 25); // Thursday

$nextBusinessDay = $date->nextWeekday();
echo $nextBusinessDay; // Outputs 2024-10-28 (Monday)

Example 6: Calculate Age from Birthdate

Calculate someone's age based on their birthdate.

CODE SAMPLE Code snippet
$birthDate = Carbon::createFromDate(1990, 10, 27);
$age = $birthDate->age; // Automatically calculates age

echo "You are $age years old."; // Outputs age based on current date

Example 7: Format Date in a Locale

Format dates in different languages using locale settings.

CODE SAMPLE Code snippet
$date = Carbon::now();
echo $date->locale('fr')->translatedFormat('l, j F Y'); // e.g., samedi, 27 octobre 2024 (in French)

Example 8: Getting Differences Between Dates

Find out the difference between two dates.

CODE SAMPLE Code snippet
$start = Carbon::createFromDate(2024, 1, 1);
$end = Carbon::createFromDate(2024, 12, 31);

$diffInDays = $start->diffInDays($end);
echo "There are $diffInDays days between $start and $end."; // 365

Example 9: Comparing Dates

Carbon makes it easy to compare two dates.

CODE SAMPLE Code snippet
$date1 = Carbon::createFromDate(2024, 1, 1);
$date2 = Carbon::createFromDate(2024, 12, 31);

if ($date1->isToday()) {
    echo "Today is January 1, 2024!";
} elseif ($date1->isFuture()) {
    echo "$date1 is in the future.";
} elseif ($date1->isPast()) {
    echo "$date1 is in the past.";
}

Example 10: Adding or Subtracting Time

You can manipulate dates by adding or subtracting time easily.

CODE SAMPLE Code snippet
$nextWeek = Carbon::now()->addWeek();
echo $nextWeek; // e.g., 2024-11-03 15:30:00

$lastMonth = Carbon::now()->subMonth();
echo $lastMonth; // e.g., 2024-09-27 15:30:00

More engineering notes

Custom Blade Directives in Laravel

Laravel allows you to create custom Blade directives, making it easier to reuse and organize common...

Route Caching to Enhance Laravel Application's Performance

Enhance route caching to improve your application's performance by speeding up route loading. ``` /...