Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How the Carbon library simplifies date calculations in Laravel applications

Initializing Variables
The code starts by initializing several variables using the @php directive.

@php
    $customIndex = 1; // Initialize custom index
    use Carbon\Carbon;
    $currentDate = Carbon::now();
    $thisMonthEarnings = 0;
    $thisWeekEarnings = 0;
    $thisYearEarnings = 0;
@endphp
Enter fullscreen mode Exit fullscreen mode

$customIndex: This variable is set to 1 and is later used to create a custom index for the displayed data.
Carbon: The Carbon class is imported, a powerful date and time library in PHP.
$currentDate: This variable stores the current date and time using Carbon.
$thisMonthEarnings, $thisWeekEarnings, $thisYearEarnings: These variables will be used to calculate and store total earnings for the current month, week, and year.
2. Iterating Through Earnings Data
The next section of the code utilizes a @foreach loop to iterate through an array of $walletsharedata, presumably containing earnings data.

@foreach($walletsharedata as $data)
    <tr>
        <!-- Displaying various attributes of earnings data -->
        <td>{{ $customIndex }}</td>
        <td>{{ $data->order_pay_date }}</td>
        <td>{{ $data->id }}</td>
        <td>{{ $data->slug }}</td>
        <td>{{ $data->pay_amount }}</td>
    </tr>

    @php
        // Increment the custom index
        $customIndex++;

        // Calculate earnings for this month, week, and year
        if (Carbon::parse($data->order_pay_date)->isCurrentMonth()) {
            $thisMonthEarnings += $data->pay_amount;
        }
        if (Carbon::parse($data->order_pay_date)->isCurrentWeek()) {
            $thisWeekEarnings += $data->pay_amount;
        }
        if (Carbon::parse($data->order_pay_date)->isCurrentYear()) {
            $thisYearEarnings += $data->pay_amount;
        }
    @endphp
@endforeach
Enter fullscreen mode Exit fullscreen mode

Inside the loop, a table row (

) is generated for each earnings data entry, displaying various attributes such as order date, ID, slug, and pay amount.
The custom index is incremented for each iteration.
The earnings for the current month, week, and year are calculated using Carbon's date comparison methods.
3. Displaying Total Earnings
After processing the earnings data, the code displays the total earnings for the current month, week, and year.
<div>
    <p>Total Earnings This Month: {{ $thisMonthEarnings }}</p>
    <p>Total Earnings This Week: {{ $thisWeekEarnings }}</p>
    <p>Total Earnings This Year: {{ $thisYearEarnings }}</p>
</div>

The tag closes the table body.
A

section displays three paragraphs (

) indicating the total earnings for the current month, week, and year.

output

Image description

Summary

*initialize variable for customIndex,thisMonthEarnings ,thisWeekEarnings *
Carbon::parse($data->order_pay_date)->isCurrentYear()) {
$thisYearEarnings += $data->pay_amount;

Top comments (0)