skip to Main Content

I know how to use date and dateTime. But in this case I want my data to be like 2023-03-03 Friday. Does laravel provides something like this?

this is my code

$table->date('estimate_time_arrival');

how can i modify it?

2

Answers


  1. You can use the timestamp or datetime column type in your migration to store the date and time information.

    $table->timestamp('estimate_time_arrival');
    

    THen for diplay you can do something like…

    $record = DB::table('your_table_name')->first();
    
    $timestamp = Carbon::parse($record->estimate_time_arrival);
    $formattedDateTime = $timestamp->format('Y-m-d l'); // Produces "2023-03-03 Friday"
    
    Login or Signup to reply.
  2. You can store values as dateTime in the database and then use model casts like this:

    class YourModel extends Model
    {
        protected $casts = [
            ...
            'estimate_time_arrival' => 'datetime:Y-m-d l',
        ];
    }
    

    You can read more about date casting and other types of casting here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search