Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Adgower's avatar
Level 14

Format date with Carbon diffForHumans

{{Carbon\Carbon::parse($events->first()->start_date)->diffForHumans()}}

Returns 4 hours from now.

The value is

2020-07-13 08:00:00

and current time on is 2020-07-12 22:42

I would like it to return "tomorrow" or even better would be ~9 hrs

0 likes
7 replies
mvd's avatar

Hi @adgower,

Does this works?

 $event = Carbon\Carbon::parse($events->first()->start_date);
 $diffInHours = $event->diffInHours(Carbon\Carbon::now());
 dd($diffInHours);
Adgower's avatar
Level 14

@mvd it returns "5" I assume hours. Technically it should be 42 minutes ago and if it rounds up 1hr ago

Adgower's avatar
Level 14

When I run

{{Carbon\Carbon::now()}}

Returns

"2020-07-13 13:50:05"

Which is off by 5 hours. I set env variable to:

APP_TIMEZONE=America/Chicago

And ran

php artisan config:clear
deladels's avatar

@adgower I don't think there's an official way of displaying 'Tomorrow' or getting carbon to return date after using diffForHumans(). Not that I know of.

Maybe what you can do is check if the date you want is equal to the date for tomorrow and display Tomorrow

You can do this by checking with the isTomorrow() carbon method.

Adgower's avatar
Adgower
OP
Best Answer
Level 14

Setting the timezone in the now fixed it for me. Is this the best solution?

{{Carbon\Carbon::now('America/Chicago')->diffForHumans($events->first()->start_date)}}

Returns

1 hour after

Which is correct! Thanks for assisting me.

BryanK's avatar

Just for reference for anyone else looking at this, base time zone should be set in your config file:

config\app.php

'timezone' => 'America/New_York',

Also, you can set Carbon with one day words (and lots of other options):

$date->endOfDay()->diffForHumans(['options' => Carbon\Carbon::ONE_DAY_WORDS]);

Carbon::ONE_DAY_WORDS (disabled by default): turns "1 day from now/ago" to "yesterday/tomorrow"

https://carbon.nesbot.com/docs/#api-humandiff

1 like

Please or to participate in this conversation.