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

diegoroga1's avatar

How orderBy date when is a variable type String

Hi. I want to sort a variable of type string but it contains date format. (10/30/2018) <- String. The orderby method sorts it as a String. any solution?

0 likes
3 replies
samuelbie's avatar

why dont you change the type of the colomn to make things easier.

tykus's avatar

What sort of data structure are you working with here?

Consider parsing the date string and normalizing the format to yyyy-mm-dd which will sort in the expected manner.

Shahrukh4's avatar

You can try something like this, which get all the post of user_id 23 with any given date,

  public function yourFunction(Request $request){
      $data = $this->post
      ->where('user_id', 23)
      ->whereDate('date', Carbon::parse($request->date)->format('Y-m-d'))
      ->get();
  }
1 like

Please or to participate in this conversation.