0

When I use diffForHumans it show 1 hour ago. When it cross 24 hour and show 1 day ago then 2 day, 3 day, 1 week, 2 week. But i want to show 1 day and 1/2/3/4 hour ago. then 2 day 1/2/3/4 hour ago.

I have been using laravel 5.7

5 Answers 5

1

use

$diffInDays = $user->created_at->diffInDays();
$showDiff = $user->created_at->diffForHumans();
if($diffInDays > 0) $showDiff .= ', '.$user->created_at->addDays($diffInDays)->diffInHours().' Hours';
echo $showDiff;
Sign up to request clarification or add additional context in comments.

Comments

1

Use ->diffForHumans(['parts' => 2]) (or more parts if you want minutes, seconds, etc.)

Comments

0

You can use longAbsoluteDiffForHumans() or longRelativeDiffForHumans(), as an example given below :

$prev = Carbon::create(2018, 2, 26, 4, 29, 43);
$now = Carbon::now();
$diff = $prev->longAbsoluteDiffForHumans($now, 6);

return $diff; // 2 years 4 months 2 weeks 2 days 11 hours 5 minutes

See the official documentation here

7 Comments

It show an error Method longAbsoluteDiffForHumans does not exist in blade file
@MizanurRahman in the blade you need to do like {{ $data->created_at->longAbsoluteDiffForHumans(6) }} use longAbsoluteDiffForHumans(6) instead of diffForHumans()
Call to a member function longAbsoluteDiffForHumans() on string
$diff = \Carbon\Carbon::create(2018, 2, 26, 4, 29, 43); $now = \Carbon\Carbon::now(); $res = $diff->longAbsoluteresForHumans($now, 6); return $res;
I tried $submitdate = date('Y-m-d, H:m:s', strtotime($submit->created_at)); $diff = \Carbon\Carbon::create($submitdate); $now = \Carbon\Carbon::now(); $res = $diff->longAbsoluteresForHumans($now, 6); return $res;
|
0

It is work with day count.

@php($diffInDays = \Carbon\Carbon::parse($user->created_at)->diffInDays())

@php($showDiff = \Carbon\Carbon::parse($user->created_at)->diffForHumans())

@if($diffInDays > 0)

@php($showDiff .= ', '.\Carbon\Carbon::parse($user->created_at)->addDays($diffInDays)->diffInHours().' Hours')

@endif

{{$showDiff}}

Comments

0

my answer is not better than the above answers, but below code worded for me as i customized the time column in the database.

 date('H', strtotime($time_column))

it's a php function, and you can change the format I mean the 'H' the first argument in date() function to the format that best suit your app.

  • another solution is using Carbon laravel time class like below code examples Carbon::parse($time_column)->hour Carbon::parse($time_column)->day

this depends the value of the $time_column

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.