Friday, March 3, 2017

Laravel compare two dates

Im trying to add an extra property to my model based on if a timestamp has passed. I tried:

public function getIsOpenAttribute()
{
    $date = new Carbon($this->closed_at);
    return $date->gt(Carbon::now());
}

And

return $date > Carbon::now();

For some reason it's always true. When I use:

public function getNowAttribute()
{
    $now = Carbon::now();

    if($this->closed_at < $now)
    {
        return $this->closed_at." < ". $now;
    }
    return $this->closed_at." >= ". $now;
}

It always returns the second return. I have used

protected $dates = ['closed_at'];

But cant get the right boolean.

How should I test if a date has passed or not?



via Sven B

Advertisement