Viewed   65 times

I want to change the date format which is fetched from database. now I got 2016-10-01{{$user->from_date}} .I want to change the format 'd-m-y' in laravel 5.3

{{ $user->from_date->format('d/m/Y')}}

 Answers

1

Try this:

date('d-m-Y', strtotime($user->from_date));

It will convert date into d-m-Y or whatever format you have given.

Note: This solution is a general solution that works for php and any of its frameworks. For a Laravel specific method, try the solution provided by Hamelraj.

Monday, November 7, 2022
1

I think you're going about this the wrong way. The data in your database doesn't need to be more human readable, only the display that a human actually interacts with.

To solve this, we will create a custom accessor method that will apply to all calls for the created_at. You can recreate this for the updated_at.

public function getCreatedAtAttribute($timestamp) {
    return CarbonCarbon::parse($timestamp)->format('M d, Y');
}

Then when you call $model->created_at your attribute will return in that format.

If for some reason you absolutely need the date stored in that format, then you need to add an attribute to your model telling it that the timestamp columns should be formatted according to a specific type, such as:

protected $dateFormat = 'M d, Y';

Sidenote
The reason that Carbon is involved is that it is applied to all of the columns generated by $table->timestamps(), so the created_at and updated_at columns.
Furthemore, if you add more columns in the model to the protected $dates = [] array, those will also automagically be handled by Carbon.

Sunday, November 27, 2022
 
trig
 
5

Try this:

<?php echo date('d F Y', strtotime($post->tanggal)); ?>

Where

  • d - The day of the month (from 01 to 31)
  • F - A full textual representation of a month (January through December)
  • Y - A four digit representation of a year
Friday, December 16, 2022
 
1

How to convert from one date format to another using SimpleDateFormat:

final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";

// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
Wednesday, November 16, 2022
 
1

The code goes something like this:

SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");

try {

String reformattedStr = myFormat.format(fromUser.parse(inputString));
} catch (ParseException e) {
e.printStackTrace();
}
Wednesday, August 3, 2022
 
stijn
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :