Mizanur Rahman
Mizanur Rahman

Reputation: 83

how to show day and hour from timestamp in laravel

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

Upvotes: 0

Views: 3374

Answers (5)

MostafaHashem
MostafaHashem

Reputation: 359

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

Upvotes: 0

KyleK
KyleK

Reputation: 5056

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

Upvotes: 1

Mizanur Rahman
Mizanur Rahman

Reputation: 83

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}}

Upvotes: 0

STA
STA

Reputation: 34688

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

Upvotes: 0

Kongulov
Kongulov

Reputation: 1161

use

$diffInDays = $user->created_at->diffInDays();
$showDiff = $user->created_at->diffForHumans();
if($diffInDays > 0) $showDiff .= ', '.$user->created_at->addDays($diffInDays)->diffInHours().' Hours';
echo $showDiff;

Upvotes: 0

Related Questions