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

arronar's avatar

Sort posts by date that has been taken from a bootstrap calendar

Hello!

I have create a small backend system to manage a web page. When i post something i add the date by using a bootstrap calendar plugin to a text input. What i wanted is to sort all posts in a descending order both in backend and frontend. It seems that this doesn't happen. The date form i'm using is the day/month/year.

Here is the table for the posts :

public function up()
    {
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('postDate');
        $table->string('priority');
        $table->string('postThumb');
        $table->string('elPostTitle');
        $table->text('elPostContent');
        $table->string('enPostTitle');
        $table->text('enPostContent');
        $table->string('uri');
        $table->rememberToken();
        $table->timestamps();
    });
}

Also here is the way i retrieve them from the Controller

public function getPosts(){
    $response = array(
        'status' => 0,
        'message' => array(),
        'posts' => array()
        );

    // Get all posts Data
    $posts = new Post;
    array_push($response['posts'], $posts::orderBy('postDate', 'DESC')->get());
    return json_encode($response);
}

And at last. Here is an example of how it returns the posts order by desc date.

24/06/2005
22/05/2006
19/08/2011
18/07/2000
17/03/2008
15/09/2015
14/07/2010
14/07/1993
13/06/2007
13/01/2016
11/07/2007
10/08/2010
10/05/2010
10/01/2012
08/07/1998
07/06/2006

Is there any quick/easy way to fix this ?

Any idea/hint is welcomed!

Thank you.

0 likes
2 replies
jekinney's avatar
Level 47

Use Carbon to parse the imputed date to a timestamp or date field in your database not a string (varchar) MySQL works better with integers and proper types then strings. Strings sorting by default is alphabetical order.

Try sorting by created at column to see the proper out put.

Snapey's avatar

use the built in timestamps?

If you must use the date, you have to convert it to a datetime format and not just save it as a string (or else you just have a string sort as you have seen)

Please or to participate in this conversation.