13

My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.

Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.

How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.

1
  • 1
    It's usually not good to do hefty data sorting in the view, but light formatting is arguably view's job. Commented Apr 23, 2021 at 6:11

5 Answers 5

34

Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.

If you're using Eloquent Models you can do it very easily:

1. Add the columns to the $dates property in your model class:

protected $dates = ['date_begin', 'date_end'];

This will ensure that the values get mutated to Carbon instances.

2. In your view files you can use the format method that Carbon offers like so:

<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}

<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}

<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}

There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.


If you're not using Eloquent models, then you can manually use Carbon to format your value like so:

{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, very good, that worked too. I thought, I should do it in the controller. :D Thank you very much!
This solution is deprecated since Laravel 7.
11

I have used the PHP solution without Carbon:

Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin to:

date('d-m-Y H:i:s', strtotime($item->date_begin))

After that, when you save it on the database add on the DB update:

date('Y-m-d H:i:s', strtotime($request->date_begin))

Comments

7
protected $casts = [
         'inicio'  => 'datetime:Y-m-d\TH:i'
        ,'entrega'  => 'datetime:Y-m-d\TH:i'
   ];

you could use it for the datetime-local field. greed you. like me if work for you

Comments

1

// attributes that should be cast to native types

 protected $casts = [
        'email_verified_at' => 'datetime'
    ];


// get formatted datetime string for email_verified_at

public function getEmailVerifiedAttribute()
 {
        if ($this->email_verified_at) {
            $date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
            return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
        } else {
            return null;
        }
    }

as Laravel document way.

Comments

0

You can use like this in blade file


{{date('Y-m-d',strtotime($invoice->invoice_created_date))}}

use date function to fix this issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.