Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yorkata's avatar

Remove time from type date

When I add new dates to my recors the 00:00:00 is displayed too. Example: 2023-03-08 00:00:00 How can I remove it? My view:

<div class="col-xs-12 col-sm-12 col-md-12">
			<div class="form-group">
			<strong>Start Date:</strong>
			<input name="start_date" type="date" value="{{ $competition->start_date }}" 
			class="form-control @error('start_date') is-invalid @enderror" id="inputStart_date" 
			placeholder="Enter Start Date">
			</div>
</div>

type="date" add a small calender to choose the dates.

This is how I show the column in my table.

 <td>{{ $competition->start_date}}</td> 

The type of the column (in my migration) is datetime

0 likes
8 replies
LaryAI's avatar
Level 58

To remove the time from the date, you can use the format() method of the Carbon library. First, you need to convert the datetime string to a Carbon instance, and then format it to display only the date part. Here's an example:

use Carbon\Carbon;

// convert datetime string to Carbon instance
$date = Carbon::parse($competition->start_date);

// format the date to display only the date part
$formattedDate = $date->format('Y-m-d');

// display the formatted date in your view
<input name="start_date" type="date" value="{{ $formattedDate }}" class="form-control @error('start_date') is-invalid @enderror" id="inputStart_date" placeholder="Enter Start Date">

In this example, we first use the parse() method of the Carbon library to convert the datetime string to a Carbon instance. Then, we use the format() method to format the date in the desired format, which is Y-m-d (year-month-day). Finally, we display the formatted date in the input field.

To display the formatted date in your table, you can use the same format() method in your view:

<td>{{ $competition->start_date->format('Y-m-d') }}</td>

This will display only the date part of the datetime string in your table.

Yorkata's avatar

@LaryAI Call to a member function format() on string Did not work :(

kokoshneta's avatar

@Yorkata See update to reply. You have to cast your date properties to dates to get it to work, otherwise it’s just a string.

Yorkata's avatar

@kokoshneta thanks for your time. I updated my model with

protected $casts = [
      'start_date' => 'datetime:Y-m-d',
      'end_date' => 'datetime:Y-m-d',
    ];

and my view with:

<td>{{ $competition->start_date->toDateString() }}</td>

But still an error Call to a member function toDateString() on null

kokoshneta's avatar

@Yorkata That’s a different error – now the date is null instead of a string. Are you sure the start date is actually set for the specific model you’re loading?

1 like

Please or to participate in this conversation.