How to format timestamp in laravel?

by arnoldo.moen , in category: PHP Frameworks , 4 months ago

How to format timestamp in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 4 months ago

@arnoldo.moen 

To format a timestamp in Laravel, you can use the Carbon library which is already included in Laravel. Here's an example of how you can format a timestamp in Laravel:

1
2
3
4
5
6
use CarbonCarbon;

$timestamp = '2022-06-15 18:30:00';
$formattedTimestamp = Carbon::parse($timestamp)->format('Y-m-d H:i:s');

echo $formattedTimestamp;


In the above example, the Carbon::parse() method is used to convert the timestamp string into a Carbon instance. Then, the format() method is used to format the timestamp in the desired format. In this case, 'Y-m-d H:i:s' is used as the format, which outputs the timestamp in the format '2022-06-15 18:30:00'.


You can use any PHP date format characters to customize the output of the timestamp based on your requirements.