How to get date from created_at field in laravel

Updated at 01-Sep-2021, By samar

How to get date from created_at field in laravel

We’ll attempt to use programming in this lesson to solve the "How to get date from created_at field in laravel" puzzle.

You can get date from created_at filed in laravel using toDateString() and format() method on created_at attribute.
  • Get date from created_at in laravel

    //toDateString() method
    $date = User::find(1)->created_at->toDateString();
    echo $date;
    
    //format() method
    $date = User::find(1)->created_at->format('Y-m-d');
    echo $date;
    
    //Output :
    2021-08-02
    

    You can change the default timestamp value of created_at attribute to in date format in laravel. This code snippet will get the record of user with id 1 and get the date from created_at attribute.

Back to code snippet queries related laravel