Search code examples
phplaravelphp-carbon

Laravel Carbon using diffInDays and now()


I managed to solve my previous problem which is counting the days left from due_date until today's date. The problem that I'm currently facing is even though the due_date is already passed, but is still counting the different days. Supposedly I want to make the output something like Overdue. To be more clearer, I show the example below:

enter image description here

For the third one, supposedly the days left should be negative or showing Overdue since today's date is 15/10/2020. Other 2 is correct I think? So how can I solve this?

complaints table

id
defect_id
created_at (date)
due_date (date)
updated_at (timestamp)

ComplaintController

public function store(Request $request)
    {
        if (count($request->defect_id) > 0) {
            foreach($request->defect_id as $item=>$v) {
                $data = array(    
                    'defect_id' => $request->defect_id[$item],               
                    'created_at' => Carbon::today()->toDateString(),
                    'due_date' => Carbon::today()->addDays(30),
                    'updated_at' => Carbon::now()->toDateTimeString(),
                );

                Complaint::insert($data);
            }
        }
        return redirect('/report-form')->with('success','Your report is submitted!');

index.blade.php

<div class="panel-heading">
   <h3 class="panel-title"><strong>Pending Report</strong></h3>
</div>
<div class="panel-body">
    <table class="table table-hover" id="report-table">
        <thead>
           <tr>
              <th>Types of Defect</th>
              <th>Report Date</th>
              <th>Due Date</th>
              <th>Days Left</th>
              <th></th>
           </tr>
        </thead>
        @foreach($complaint as $c)
           <tr>
              <td>{{$c->defect->name}}</td>
              <td>{{$c->created_at->format("d/m/yy")}}</td>
              <td>{{date('d/m/yy', strtotime($c->due_date))}}</td>
              @if( \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) <= 10 && \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) > 0 )
                  <td class="text-danger">{{ \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) }} days</td>
              @elseif( \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) > 10)
                  <td class="text-success">{{ \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) }} days</td>
              @else
                  <td class="text-danger">Overdue</td>
              @endif
              <td><a href="/report/{{$c->id}}/view" class="btn btn-primary">View</a></td>
           </tr>
        @endforeach
    </table>
</div>

There is nothing declared in the model for the date. I'm hoping for someone to teach and show me one by one what I need to do.

--Update--

@if( \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) <= 10 && \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) > 0 )
    <td class="text-danger">{{ \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) }} days</td>
 @elseif( \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) > 10)
    <td class="text-success">{{ \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) }} days</td>
@elseif( \Carbon\Carbon::parse($c->due_date)->diffInDays(now()) < 0)
    <td class="text-success">{{ \Carbon\Carbon::parse($c->due_date)->diffInDays(now(), false) }} days</td>
@endif

Solution

  • You can try and make it cleaner by separating out the logic a little.

    Make use of the isPast() and diffForHumans() carbon methods.

    Then use ternary and if statements later based on the logic laid out beforehand.

    Something like:

    @foreach($complaint as $c)
        @php
            $dueDate = \Carbon\Carbon::parse($c->due_date);
            $dueSoon = $dueDate->diffInDays(now()) <= 10 && $dueDate->diffInDays(now()) > 0;
            $isPast = $dueDate->isPast();
        @endphp
        <tr>
            <td>{{$c->defect->name}}</td>
            <td>{{$c->created_at->format("d/m/yy")}}</td>
            <td>{{date('d/m/yy', strtotime($c->due_date))}}</td>
            <td class="{{ ($isPast || $dueSoon) ? 'text-danger' : 'text-success' }}">
                @if($isPast)
                    Overdue
                @else
                    {{ $dueDate->diffForHumans() }}
                @endif
            </td>
            <td><a href="/report/{{$c->id}}/view" class="btn btn-primary">View</a></td>
        </tr>
    @endforeach