skip to Main Content

I am unsure if I should use the Carbon date object directly in the where() function. Or should I use the timestamp of the Carbon (date) object?

$todayMarketCloseTime = Carbon::now('Asia/Karachi')->hour(15)->minute(10)->second(00);

// Should I get the timestamp on the Carbon date object before passing it to where function?

$ratiosUpdatedAt = $todayMarketCloseTime->getTimestamp();

return Ratio::where('updated_at', '>=', $ratiosUpdatedAt)
->orderBy('ratio', 'desc')
->get();

I am expecting to get all dates fom Ratios table whose updated date is greater than or equal to the Closing time of the Stock Market today.

2

Answers


  1. You can use either of those approaches; it comes down to preference. However, you could argue that using a Carbon date object is more human-readable and can make your code easier to understand,

    Using a Carbon date object directly:

    $todayMarketCloseTime = Carbon::now('Asia/Karachi')
        ->hour(15)->minute(10)->second(00);
    
    return Ratio::where('updated_at', '>=', $todayMarketCloseTime)
        ->orderBy('ratio', 'desc')
        ->get();
    
    Login or Signup to reply.
  2. In Laravel’s Eloquent, date comparisons are generally made using the native date format of your database. If you’re using a typical updated_at column, it is a datetime type column that will store values in the format YYYY-MM-DD HH:MM:SS.

    In the given code, you don’t need to convert your Carbon object to a timestamp using getTimestamp(). Instead, you should use the default string representation that Carbon provides for a MySQL datetime type.

    $todayMarketCloseTime = Carbon::now('Asia/Karachi')->hour(15)->minute(10)->second(00);
    
    return Ratio::where('updated_at', '>=', $todayMarketCloseTime)
        ->orderBy('ratio', 'desc')
        ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search