Vipul Nandan
Vipul Nandan

Reputation: 107

How to update cache once a day on the day (00:00:00) in Laravel 5?

I have a data set that has to be updated only once every 24 hours. This should happen with the first request of the day at or after 00:00:00. how do we set the cache remember condition for this as it only allows for setting minutes not the time?

Upvotes: 3

Views: 7674

Answers (3)

Lucas
Lucas

Reputation: 11

Just correcting the most upvoted answer : Carbon::now()->endOfDay(); doesnt return the current date at 23h59s but rather the current date at 23h 59s 999ms.

Carbon::now()->endOfDay(); is closer to 00h than Carbon::now()->endOfDay()->addSecond(); which equals 00h 00m 999ms.

Upvotes: 0

maxwilms
maxwilms

Reputation: 2024

You can use Carbon for that. In the Laravel documentation we can find an example to expire a cache after 10 minutes:

$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);

All you want it to determine the end of the day. Luckily Carbon offers everything:

$expiresAt = Carbon::now()->endOfDay();

This will give you the last second of the current day 23:59:59. Just add one second to satisfy your requirement:

$expiresAt = Carbon::now()->endOfDay()->addSecond();

Upvotes: 9

Pedro Silva
Pedro Silva

Reputation: 263

Not the complete answer, I was trying to find a way to cache at start of day OR first request of day. Going by append date to predefined key

So what you want is simple, if you want a simple solution you can add a table in your DB and it simply has two attributes: id and date. From here when someone login you check if there was a login that day, if not you do what you have to do.

EDIT

well when someone logs-in you update the date in the table.

Upvotes: 0

Related Questions