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.
{{ $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');
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';
}
}
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:
timestamps()
be saved as is i.e. '2016-11-14 12:19:49'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.
Source: Stackoverflow.com