How to only fetch time from timestamp in laravel?

by filiberto , in category: PHP Frameworks , 19 days ago

How to only fetch time from timestamp in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by shyann , 18 days ago


In Laravel, you can fetch only the time from a timestamp using the format() method provided by Carbon, which is the library used for handling dates and times in Laravel.

You can achieve this by accessing the timestamp column from your database and calling the format() method to retrieve only the time portion. Here is an example of how you can do this:

// Assuming you have a model called Post that has a 'created_at' timestamp column
$post = Post::find(1);

// Get the time portion of the timestamp
$time = $post->created_at->format('H:i:s');

// Output the time
echo $time;

In the example above, we fetched a Post model instance from the database and accessed the created_at timestamp column. We then used the format() method with the 'H:i:s' format to retrieve only the time portion (hours, minutes, and seconds) from the timestamp.

You can adjust the format string in the format() method to customize the output based on your requirements.