Using Carbon to return a human readable datetime difference
Asked Answered
F

5

76

I'm using Laravel 4 to create my project.

I am currently building the comments section and I want to display how long ago the post was created, kind of like Facebook's '10 mins ago' & '2 weeks ago' etc.

I have done a little bit of research and found that a package called Carbon can do this.

After reading the Laravel doc's, it says:

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime class.

But when I return a date column that I have created, it doesn't display it like on Facebook.

The code that I'm using is:

return array('time');

Has any body used this Carbon package that could give me a hand in doing what I need, I'm quite confused.

Fluff answered 23/6, 2013 at 11:19 Comment(0)
S
129

If you read the Carbon docs to get what you want you call the diffForHumans() method.

<?php echo \Carbon\Carbon::createFromTimeStamp(strtotime($comment->created_at))->diffForHumans() ?>
Sympathy answered 23/6, 2013 at 11:47 Comment(5)
It's actually much simpler as the quoted bit from Laravel's documentation in the question explains; $comment->created_at is already a Carbon object by default so all you need to do is: $bookmark->created_at->diffForHumans()Socher
I am using laravel 5.2 and used the same method but it is not showing correct result. e.g If I post comment 2seconds ago, it displays '4 hours ago'.Expectancy
@JoelMellon, how do you know this gem, are you working on laravel? damn...Episodic
@JoelMellon Call to a member function diffForHumans() on nullRapscallion
@FernandoTorres Your model's created_at attribute is null. It needs to be set for this to work. My guess is that your model wasn't persisted to the database, ie. wasn't created, or your migration doesn't set a default value for created_at.Socher
C
164

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon. So, your code should be just like this:

$comment->created_at->diffForHumans();

It's very cool. It'll produce string like 2 minutes ago or 1 day ago. Plurar or singular, seconds, minutes, hours, days, weeks, or years, it runs automatically. I've tested it on Laravel version 4.1.24.

Crossly answered 22/5, 2014 at 9:11 Comment(4)
It possible to get the left time?Medlock
The Carbon documentation said ... but also does 'from now', 'after' and 'before'.Crossly
I recieve Call to a member function diffForHumans() on nullRapscallion
@FernandoTorres your date must be null, check that first and also after that Carbon::parse('your date here')->diffForHumans();Handicraft
S
129

If you read the Carbon docs to get what you want you call the diffForHumans() method.

<?php echo \Carbon\Carbon::createFromTimeStamp(strtotime($comment->created_at))->diffForHumans() ?>
Sympathy answered 23/6, 2013 at 11:47 Comment(5)
It's actually much simpler as the quoted bit from Laravel's documentation in the question explains; $comment->created_at is already a Carbon object by default so all you need to do is: $bookmark->created_at->diffForHumans()Socher
I am using laravel 5.2 and used the same method but it is not showing correct result. e.g If I post comment 2seconds ago, it displays '4 hours ago'.Expectancy
@JoelMellon, how do you know this gem, are you working on laravel? damn...Episodic
@JoelMellon Call to a member function diffForHumans() on nullRapscallion
@FernandoTorres Your model's created_at attribute is null. It needs to be set for this to work. My guess is that your model wasn't persisted to the database, ie. wasn't created, or your migration doesn't set a default value for created_at.Socher
F
10

For any version of Laravel

$message->updated_at->diffForHumans();
Franckot answered 8/1, 2018 at 12:15 Comment(1)
Answered 5 years beforeInduplicate
M
4
Carbon::parse($p->created_at)->diffForHumans();
Midterm answered 9/11, 2017 at 7:31 Comment(0)
C
2

use this code for time ago :

    public function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
         'y' => 'year',
         'm' => 'month',
         'w' => 'week',
         'd' => 'day',
         'h' => 'hour',
         'i' => 'minute',
         's' => 'second',
     );
     foreach ($string as $k => &$v) {
         if ($diff->$k) {
             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
         } else {
             unset($string[$k]);
         }
     }

     if (!$full) $string = array_slice($string, 0, 1);
     return $string ? implode(', ', $string) . ' ago' : 'just now';
 }
Chaffee answered 22/3, 2016 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.