Reputation: 499
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.
Upvotes: 46
Views: 198505
Reputation: 4527
In laravel 8 to 10
When setting the
protected $timeStamps = 'Y-m-d H:i:s';
This only affects when you update
or create
but when you get/fetch
from an eloquent
the format will always have a timestamp with timezone which is the default
sample:
2023-06-14T06:27:18.000000Z
// on your Model.php
use use DateTimeInterface;
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
Everytime you user Eloquent to query the timestamps
if enabled will now have the Y-m-d H:i:s
format
if you like it to be in effect to all model, Just create a BaseModel
with the following property
then Extend all your models
Read More at Laravel 10 - Date Casting
Upvotes: 1
Reputation: 21
Casting is the proper way to do this in Laravel 9+ and regardless of your front-end; blade, vue, react... the variables will be treated properly.
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d',
'json' => 'object',
'active' => 'boolean',
];
https://laravel.com/docs/9.x/eloquent-mutators#date-casting
Upvotes: 2
Reputation: 1
LARAVEL 9^
in your model add this.
use Carbon\Carbon;
protected $dates = ['created_at','updated_at'];
protected function serializeDate(DateTimeInterface $dates)
{
//return Carbon::createFromFormat('Y-m-d H:i:s', $dates)->diffForHumans();
OR
//return Carbon::createFromFormat('Y-m-d H:i:s', $dates)->format('Y-m-d');
}
Upvotes: -1
Reputation: 1
The easiest way is just doing it directly in the blade file
$post->created_at->format('Y-m-d')
Upvotes: 0
Reputation: 1
For newers versions of laravel, you just need to declare it in your Model, like this:
class Your_class_name extends Model {
protected $dateFormat = 'Y-m-d';
...
}
Upvotes: 0
Reputation: 2034
If anyone is looking for a simple solution in Laravel 5.3:
Let default 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))
Upvotes: 7
Reputation: 316
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
Upvotes: 8
Reputation: 63
Use Carbon\Carbon;
$targetDate = "2014-06-26 04:07:31";
Carbon::parse($targetDate)->format('Y-m-d');
Upvotes: 0
Reputation: 1942
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();
Upvotes: 0
Reputation: 259
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");
Upvotes: 9
Reputation: 12731
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';
}
Upvotes: 13
Reputation: 962
{{ $post->created_at }}
will return '2014-06-26 04:07:31'
The solution is
{{ $post->created_at->format('Y-m-d') }}
Upvotes: 78
Reputation: 146201
In your Post
model add two accessor methods like this:
public function getCreatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
public function getUpdatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}
Now every time you use these properties from your model to show a date these will be presented differently, just the date without the time, for example:
$post = Post::find(1);
echo $post->created_at; // only Y-m-d formatted date will be displayed
So you don't need to change the original type in the database. to change the type
in your database you need to change it to Date
from Timestamp
and you need to do it from your migration (If your using at all) or directly into your database if you are not using migration. The timestamps()
method adds these fields (using Migration) and to change these fields during the migration you need to remove the timestamps()
method and use date()
instead, for example:
$table->date('created_at');
$table->date('updated_at');
Upvotes: 91