LaserBeak
LaserBeak

Reputation: 3285

Change date output format, using Laravel and Carbon

The MySQL database stores time in Y-m-d H:i:s i.e 2000-12-32 00:00:00 format , UTC.

I am wondering if there's some way I can convert the created_at dateTime property on the $order objects returned from the database, so it would instead send a dateTime string to the client as such: 31/04/16 14:25pm and without seconds.

for($i = 0;  $i < sizeof($orders); $i++ ) { 
    $orders[$i]->created_at = $orders[$i]->created_at->format('d/m/y H:i');
}

Upvotes: 0

Views: 3940

Answers (3)

Islam Al-Rayan
Islam Al-Rayan

Reputation: 396

Welcome to Accessors and Mutators:

Proper way of doing this using Laravel is by defining an Accessor: The accessor will automatically be called by Eloquent when attempting to retrieve the value of your fields. This means that you don't have to loop over a set of objects and then convert the values, simply define an Accessor in your model and you're good to go.

Solution #1:

So for your date field add the following method to your Orders Model

public function getCreatedAtAttribute($value)
{
    return $value->format('d/m/y H:i a');
}

The name of an Accessor should be getFieldAttribute where you replace field with your corresponding column name. This way you don't have to loop over the results.

Note: do bare in mind that by doing so, you are now getting the created_at as a string not a Carbon object anymore in your model, so how can this affect you? if you're doing some comparison somewhere else, you can also use a Mutator to set the field value more on Accessors and Mutators can be found here https://laravel.com/docs/5.1/eloquent-mutators

Solution #2:

Yeap, basically loop over all of your array and do the conversion.

for($i = 0;  $i < count($orders); $i++ ) {
    $orders[$i]->created_at = $orders[$i]->created_at->format('d/m/y H:i a');
}

Update:

if you're experiencing errors like "The separation symbol could not be found" or "Unexpected data found." adjust the solutions like this:

Solution #1:

public function getCreatedAtAttribute($value)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value->toDateTimeString())->format('d/m/y H:i a');
}

Solution #2:

for($i = 0;  $i < count($orders); $i++ ) {
    $orders[$i]->created_at = Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $orders[$i]->created_at->toDateTimeString())->format('d/m/y H:i a');
}

Upvotes: 2

haakym
haakym

Reputation: 12368

Assuming $orders is an eloquent model, the created_at and updated_at attributes are automatically converted to carbon instances, so you can already run carbon methods on them.

See the docs on this topic: https://laravel.com/docs/5.2/eloquent-mutators#date-mutators

So, your code:

$orders[$i]->created_at->format('d/m/y H:i');

Should work just fine!

You could always add a helper method in your Order model to make it a bit more readable too, something like this:

public function createdAtTimeStamp()
{
    return $this->created_at->format('d/m/y H:i');
}

Then you can access it like so:

$orders[$i]->createdAtTimeStamp()

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5445

you are right,you can format created_at time

foreach($orders as $order){
echo $order->created_at->format('d/m/y H:i a');
}

Upvotes: 0

Related Questions