Table of contents

PHP Carbon Formatting

In this example, I will show you an example of how to use Carbon Formatting dates in PHP that are readable for humans.

 

See the example below:

 

PHP Carbon Format to Date Example

<?php

$date = Carbon::now();

echo $date->toDateString();
//Result: 2022-05-06

 

PHP Carbon Format to Date & Time Example

<?php

$date = Carbon::now();

echo $date->toDateTimeString();
//Result: 2022-05-06 07:20:48

 

PHP Carbon Formatted Example

<?php

$date = Carbon::now();

echo $date->toFormattedDateString();
//Result: May 6, 2022

 

PHP Carbon Format to Time Example

<?php

$date = Carbon::now();

echo $date->toTimeString();
//Result: 07:24:08

 

PHP Carbon Format to Day, Date, and Time

<?php

$date = Carbon::now();

echo $date->toDayDateTimeString();
//Result: Fri, May 6, 2022 7:25 AM

 

PHP Carbon Format Days Ago

<?php

echo Carbon::now()->subDays(3)->diffForHumans();
//Result: 3 days ago

 

PHP Carbon Format Weeks Ago

<?php

echo Carbon::now()->subWeek(3)->diffForHumans();
//Result: 3 weeks ago

 

PHP Carbon Format Months Ago

<?php

echo Carbon::now()->subMonth(3)->diffForHumans();
//Result: 3 months ago

 

PHP Carbon Format Long Relative for Humans

<?php

echo Carbon::create('2022')->longRelativeDiffForHumans('2019');
//Result: 4 months 5 days 7 hours 31 minutes 21 seconds ago

Â