skip to Main Content

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

2

Answers


  1. Parse $lastTimeFocused using Carbon and then make your comparison:

    $newLikes = Like::where('liked_id','=',$request['user_id'])->where('created_at','>',new CarbonCarbon($lastTimeFocused))->get();
    
    Login or Signup to reply.
  2. You should format date and time before compare

    You can format using Carbon library

    $lastTimeFocused = CarbonCarbon::parse($lastTimeFocused)->toDateTimeString();
    
    $newLikes = Like::where('liked_id','=',$request['user_id'])->where('created_at','>',$lastTimeFocused)->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search