Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

successdav's avatar

Retrieve Items that are 5 days to the end date.

Hi. In my application I need to retrieve all the items that are 5 days to the end date.

$items = DB::table('items')
                ->whereRaw('DATEDIFF(end_date, NOW()) <= 5')
                ->get();

The method below will do the job. but the problem here is the end date is not stored on the databse.

The end date for each model is return from a method.


    public function getNextDueDate($format = true)
    {
        $start = $this->getRepaymentCycleStartDate();

        $durationSpent = $this->getDurationSpent();

        $durationToAdd = $durationSpent < $this->duration ? $durationSpent + 1 : $this->duration;

        $date = match($this->category->duration_period) {
            'Years' => $start->addYears($durationToAdd),
            'Months' => $start->addMonths($durationToAdd),
            'Weeks' => $start->addWeeks($durationToAdd),
        };

        return $format ? $date->format('d M Y') : $date;
    }
0 likes
2 replies
PovilasKorop's avatar

I would add the column to the database, auto-fill it for new records, and maybe periodically recalculate and update it if needed, with some scheduled command.

1 like

Please or to participate in this conversation.