How to format timestamp in laravel?

by arnoldo.moen , in category: PHP Frameworks , 13 days ago

How to format timestamp in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 12 days ago


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:

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.