s0m3d3v
s0m3d3v

Reputation: 177

Display total days between two dates if status is completed

I am trying to count the number of days between two dates the carbon::now and $start_date and when the status == complete the counting of days stops then get the total number of days.

if ($this->status === 'COMPLETED') {
    $now = Carbon::now();
    $start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
    $this->start_date_to_current_date = $start_date->diffInDays($now, true);
}

But the problem is, the days still continues to count even the status is completed like for example the total days is 3 then in the next day it becomes 4 then ive tried doing this:

if ($this->status == null && $this->status === 'COMPLETED') {
    $now = Carbon::now();
    $start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
    $this->start_date_to_current_date = $start_date->diffInDays($now, true);
}

But the value becomes zero. why is that ? :/

Upvotes: 0

Views: 65

Answers (2)

Zeshan
Zeshan

Reputation: 2657

You need to log the complete date somewhere, and use it instead of now().

For instance:

if ($this->status === 'COMPLETED') {
$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);
$start_date->diffInDays($this->completed_date); // completed_date should come from db.
}

The problem here is that you're using the current date as complete date which is obviously changing everyday, hence you get different result each day.

Upvotes: 2

EZone Lai
EZone Lai

Reputation: 106

let it only do that when you not yet, and do not repeat do it.

`if ($this->status == null && $this->status !== 'COMPLETED') {`

`$now = Carbon::now();`

`$start_date = Carbon::createFromFormat('Y-m-d', $this->start_date);`

`$this->start_date_to_current_date = $start_date->diffInDays($now, true);`

}

Upvotes: 0

Related Questions