Support the ongoing development of Laravel.io →
Database Eloquent

Hi,

I've been raging for a few hours trying to achieve this.

'created_at' and 'updated_at' both have this method from pretty much anywhere. So I thought it would be pretty straightforward to have it working with another timestamp.

$table->timestamp('due_date');

I tried it without any accessors but then I tried to workaround it that way:

public function due_date()
{
    return Carbon::createFromDate($this->due_date);
}

But it doesn't work anyway since I always get this weird message:

Unexpected data found. Unexpected data found. Unexpected data found. Trailing data

So the question is: How do I use ->diffForHumans() on other timestamp fields than created_at and updated_at?

Thanks and have a nice day.

Last updated 3 years ago.
0

You should be able to define a due date in the getDates() function and have it accessible as a Carbon instance: http://laravel.com/docs/eloquent#date-mutators

Have you tried dumping the current value of $this->due_date and see what it's looks like? I suspect it might not be formatted in a way Carbon expects.

Last updated 3 years ago.
0

EDIT: Ok, not broken. I had assumed the date string coming from Sqlite was a standard formatted date, but it seems sqlite treats datetime fields as an unvalidated string, so didn't complain when I inserted the data (which is 'datey' enough I thought it had passed some kind of validation)

This still seems to be broken for me?:

Lets add a datetime:

$table->dateTime('taken');

Now we can output in a view

{{var_dump($model->taken)}}

string(19) "2010:03:21 13:15:14"

So far so good, but that date is ugly. So we add to the model, this should mean we get a carbon instance instead of a string:

	public function getDates() { return array('created_at', 'updated_at', 'deleted_at', 'taken'); }

And BOOM "ErrorException Unexpected data found. Unexpected data found."

This is carbon.php, line 358. The function is

createFromFormat($format, $time, $tz = null);

but the error message 'Unexpected data found' is from PHPs dateTime's createFromFormat .

Looking at at the arguments we have:

Y-m-d H:i:s -> 2010:03:21 13:15:14

So I suspect the problem is the format has dashes and the date string colons. The format comes from illuminate/database/grammar.php, the date string from sqlite. Solution is to add to the sqlite grammer class:

public function getDateFormat()
	{
		return 'Y:m:d H:i:s';
	}

This fixes it for me, but I am not sure if this is simply a quirk of my environment (standard apache2/php5/sqlite from ubuntu 14.04) or a larger bug.

Last updated 3 years ago.
0

You could put this in your model:

protected $dates = ['due_date'];
Last updated 3 years ago.
0

Carbon::createFromFormat('Y-m-d H:i:s', $var)->diffForHumans();

the documentation on Carbon is quite extensive and clear on how to use it. https://github.com/briannesbitt/Carbon

0

Sign in to participate in this thread!

Eventy

Your banner here too?

tbergeron tbergeron Joined 6 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.