How to only fetch time from timestamp in laravel?

by elise_daugherty , in category: PHP Frameworks , 4 months ago

How to only fetch time from timestamp in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a month ago

@elise_daugherty 

To only fetch the time from a timestamp in Laravel, you can use the Carbon library, which is included by default in Laravel. Here's how you can extract the time from a timestamp:

1
2
3
4
5
6
7
use CarbonCarbon;

$timestamp = Carbon::now(); // Your timestamp variable

$time = $timestamp->format('H:i:s'); // Extracting the time in HH:MM:SS format

dd($time); // Displaying the time


In this example, we are using the Carbon class to manipulate the timestamp. The format() method allows us to specify the desired format for the output. In this case, we are extracting the time in the HH:MM:SS format.


You can adjust the format to suit your needs by referring to the Carbon documentation for more options: https://carbon.nesbot.com/docs/