[php] How to change default format at created_at and updated_at value laravel

I am new in Laravel. I am creating a application with laravel. When i creating a post then the values of "created_at" and 'updated_at" are look like this:

2014-06-26 04:07:31
2014-06-26 04:07:31

But i want this values without time look like this :

2014-06-26
2014-06-26

Only date without time.

Is it possible ???

Please help me.

My Post Model:

<?php

class Post extends \Eloquent {

    protected $fillable = array('title', 'body', 'meta','reporter', 'slug', 'image','top');

    public static $rules = array(
        'title'=>'required|min:2',
        'body'=>'required|min:20',
        'reporter'=> 'required|min:2',
        'image'=>'image|mimes:jpeg,jpg,bmp,png,gif'
    );

    public function categories()
    {
        return $this->belongsToMany('Category');
    }

}

My Post Controller store:

public function store()
{
    $validator = Validator::make(Input::all(), Post::$rules);

    if ($validator->passes()) {
        $post = new Post;
        $post->title = Input::get('title');
        $post->body = Input::get('body');
        $post->reporter = Input::get('reporter');
        $post->meta = Input::get('meta');
        $post->slug = Input::get('title');
        $post->top = Input::get('top');

        $image = Input::file('image');
        if ($image) {
            $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize(250, 145)->save('public/images/postimages/'.$filename);
            $post->image = 'images/postimages/'.$filename;
        }


        $categories = Input::get('categories');

        $post->save();

        $post->categories()->sync($categories);

        return Redirect::route('admin.posts.index')
            ->with('message', 'Product Created');
    }

    return Redirect::back()
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

Please Help me.

This question is related to php laravel-4 timestamp eloquent

The answer is


{{ $post->created_at }}

will return '2014-06-26 04:07:31'

The solution is

{{ $post->created_at->format('Y-m-d') }}

In laravel 6 the latest one can easily add in the "APP/user.php" model:

/**
 * The storage format of the model's date columns.
 *
 * @var string
 */
protected $dateFormat = 'U';

And in schema one can add

    $table->integer('created_at')->nullable();
    $table->integer('updated_at')->nullable();

You can also try like this.

Use Carbon\Carbon;


$created_at = "2014-06-26 04:07:31";

$date = Carbon::parse($created_at);

echo $date->format("Y-m-d");

You could use

protected $casts = [
    'created_at' => "datetime:Y-m-d\TH:iPZ",
];

in your model class or any format following this link https://www.php.net/manual/en/datetime.format.php


Use Carbon\Carbon;

$targetDate =  "2014-06-26 04:07:31";
Carbon::parse($targetDate)->format('Y-m-d');

Laravel 4.x and 5.0

To change the time in the database use: http://laravel.com/docs/4.2/eloquent#timestamps

Providing A Custom Timestamp Format

If you wish to customize the format of your timestamps, you may override the getDateFormat method in your model:

class User extends Eloquent {

    protected function getDateFormat()
    {
        return 'U';
    }

}

Laravel 5.1+

https://laravel.com/docs/5.1/eloquent

If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

If anyone is looking for a simple solution in Laravel 5.3:

  1. Let default timestamps() be saved as is i.e. '2016-11-14 12:19:49'
  2. In your views, format the field as below (or as required):

    date('F d, Y', strtotime($list->created_at))
    

It worked for me very well for me.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel-4

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel Can I do Model->where('id', ARRAY) multiple where conditions? how to fix stream_socket_enable_crypto(): SSL operation failed with code 1 Rollback one specific migration in Laravel How can I resolve "Your requirements could not be resolved to an installable set of packages" error? Define the selected option with the old input in Laravel / Blade Redirect to external URL with return in laravel laravel the requested url was not found on this server

Examples related to timestamp

concat yesterdays date with a specific time How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Pandas: Convert Timestamp to datetime.date Spark DataFrame TimestampType - how to get Year, Month, Day values from field? What exactly does the T and Z mean in timestamp? What does this format means T00:00:00.000Z? Swift - iOS - Dates and times in different format Convert timestamp to string Timestamp with a millisecond precision: How to save them in MySQL

Examples related to eloquent

Eloquent: find() and where() usage laravel How to select specific columns in laravel eloquent Laravel Eloquent where field is X or null Laravel Eloquent limit and offset laravel collection to array Eloquent get only one column as an array Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0 Laravel 5.2 - pluck() method returns array Eloquent ORM laravel 5 Get Array of ids eloquent laravel: How to get a row count from a ->get()