6

I've looked around, but haven't been able to find a clear way of returning Laravel timestamps / datetimes in a particular format. It doesn't really matter to me how it's saved in the database, but when I'm pulling the data, I'd like to format it so that it can be parsed by JavaScript. Does someone know of a good way that doesn't involve having to create multiple accessors?

Edit: To add more detail, I'm actually using AngularJS and would like the date returned in a format so that it can be understood by Angular and its date filter. With the current format, the date filter doesn't work because it can't parse the date, from what I can tell.

4
  • How do you intend for them to be "parsed by JavaScript"? You should never parse strings using the built–in ECMAScript Date constructor or Date.parse method as they are largely implementation dependent and notoriously fickle. The most robust method is to supply a time value (e.g. UNIX time value) that can be converted very easily. Commented Nov 12, 2015 at 6:53
  • Ah, I wasn't aware that the Date constructor wasn't reliable. I think the main issue is that a JavaScript Date object can't be created using the time format Laravel returns. Commented Nov 12, 2015 at 7:02
  • It's easy to write a simple parser (2 or 3 lines) if the string is in a specific format. Commented Nov 12, 2015 at 7:06
  • Do you solved the problem? Commented Nov 14, 2015 at 12:30

3 Answers 3

1

To define an accessor, create a getFooAttribute method on your model where Foo is the "camel" cased name of the column you wish to access. So, you should define an accessor for the test_date (or anything else) attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of test_date.

(In the example I'll define it in the User model)

<?php

namespace App;

use Carbon\Carbon;

class User extends Model {
    protected $table = 'users';

    public function getTestDateAttribute($date) {
        //What format do you store in db?
        $storedformat = createFromFormat('Y-m-d H:i:s', $date);
        //What format would you like to show?
        $customformat = $storedformat->format('Y.m.d. H:i:s');
        return $customformat;
    }
}

Then you'll see the following format by default:

$user = User::find(1);
$user->test_date; //2015.11.12. 8:50:20


If you'd like to write simplier code, you should use Traits.

Let's define the DateTrait in App\DateTrait.php file:

<?php

trait DateTrait {
    public function getTestDateAttribute($date) {
        //What format do you store in db?
         $storedformat = createFromFormat('Y-m-d H:i:s', $date);
         //What format would you like to show?
         $customformat = $storedformat->format('Y.m.d. H:i:s');
         return $customformat;
    }
 }

Then use it every model where you'd like to format the test_date column.

<?php

namespace App;

use Carbon\Carbon;

class User extends Model {
    use App\DateTrait;
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

I figured that this would work. Is there a way to have the format apply to all timestamps? I have other columns that are DateTime with different column names, but would like the same thing accomplished without having to create all those accessor's.
1

In your view just do something like this:

$variable->created_at->format('m-d-y')

1 Comment

I edited my question to be more specific, but I'm using AngularJS, so no Blade for this project.
0

This page here may be useful for getting familiar with Carbon dates and how you can use and format them.

Regarding the parsing of them to a format Javascript can interpret, perhaps you could do something like this - assuming it is coming from an eloquent object you have pulled in to a view or something like that:

{{ (new Carbon\Carbon($dataPassedIn->created_at))->format('F d, Y H:ia') }}

Which would return a date like this September 02, 2015 09:43am, although I'm not sure of the exact format you would need for parsing in to javascript.

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.