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

Jonjie's avatar
Level 12

Get record of the item before specific date

How to get a previous record or data using the specific date?

Example: If I have a data from Nov 20 - 23, I want the data of Nov 19 (which is Monday) to be the the last data before it. So ...

Previous Dates: ['Nov 8', 'Nov 9', 'Nov 14', 'Nov 15', 'Nov 16']

Previous Data: [11, 20, 30, 13, 15]

Dates: ['Nov 19', 'Nov 20', 'Nov 21', 'Nov 22', 'Nov 23']

Data: [15, 18, 10, 20, 32]

The 15 in Data should be the previous record.

Support Reference (Stackoverflow):

https://stackoverflow.com/questions/53514466/how-to-get-data-from-previous-record-in-laravel/53516509#53516509

0 likes
6 replies
burlresearch's avatar

I have these 2 lines which may help you:

>>> Carbon\Carbon::parse( 'Nov 21' )->day
=> 21
>>> collect([15, 18, 10, 20, 32])->filter(function($i) { return $i < 21; })->max()
=> 20
Jonjie's avatar
Level 12

Hi @burlresearch . THanks for the quick response. Any explanation with your anwer? You might also want to check my stackoverflow question.

burlresearch's avatar

What I posted is just 2 snippets to help with the code you posted here. The question on SO seems rather different. After reading it, I suspect there is more going with what you want. I'd like to help, but you'll need to clarify.

Does the current day-of-week matter?

IE if today is Tue, but has no data, do you want the data from last Fri, or do you want the data from last Tue (assuming both have data)?

Jonjie's avatar
Level 12

Sorry for the confusion. What I want is the quantity/data before the date that has no quantity/data.

burlresearch's avatar

sorry dude, now I'm even more confused.

Do you want the last 5 entries before the current, empty, one? Or just the last non-null one? This doesn't seem to be as hard as you're making it?

Please or to participate in this conversation.