0

in my laravel app I'm trying to check if new records were created after a certain timestam (lastTimeFocused) the thing is I'm getting wrong results...

$newLikes = Like::where('liked_id','=',$request['user_id'])->where('created_at','>',$lastTimeFocused)->get();

Where lastTimeFocused is the last time the app screen was focused, here I'm gettign lots of results that were presumably created "after" but if I check these records the created_at timestamp shows it's not the case...

"lastTimeFocused": 2023-08-03T12:10:33.195Z,
created_at": "2023-08-03T11:56:32.000000Z

How is created_at supposed to be after lastTimeFocused? Maybe I have to format before compare?

I get lastTimeFocused with a simple javascript new Date() on the frontend

4
  • Could this be a timezone issue? i.e. if your connection with the database is using a different timezone the database date will be adjusted based on the connection timezone Commented Aug 3, 2023 at 13:11
  • I'm from Spain so probably, how could I approach this then? I will also have many users on different timezones... Commented Aug 3, 2023 at 13:23
  • So assuming the lastTimeFocused you shared is actually correct (remember the Z at the end means "Zero offset" i.e. UTC time) then set your Laravel timezone settings to UTC in config/app.php to ensure everything is in UTC on the server side. You can correct for the timezone in JavaScript. The browser is much more likely to know the timezone the user has set (and remember the user might choose to set a different timezone to what their location's IP is for good reasons). I would recommend to use UTC for everything on the server and correct for timezone in the browser. Commented Aug 3, 2023 at 13:26
  • Since created_at should be cast into a date type by Eloquent, this means $lastTimeFocused has to be something that Carbon can parse into a date. You can try using the query log. This will show the SQL query and might give you some ideas as to where the problem is. Commented Aug 3, 2023 at 19:53

2 Answers 2

0

You should format date and time before compare

You can format using Carbon library

$lastTimeFocused = \Carbon\Carbon::parse($lastTimeFocused)->toDateTimeString();

$newLikes = Like::where('liked_id','=',$request['user_id'])->where('created_at','>',$lastTimeFocused)->get();
Sign up to request clarification or add additional context in comments.

2 Comments

Still getting wrong results
try $lastTimeFocused = \Carbon\Carbon::parse($lastTimeFocused)->setTimezone('UTC')->toDateTimeString();
0

Parse $lastTimeFocused using Carbon and then make your comparison:

$newLikes = Like::where('liked_id','=',$request['user_id'])->where('created_at','>',new \Carbon\Carbon($lastTimeFocused))->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.