ホーム > Laravel > How to Convert Timestamp to Date Type with Carbon
Laravel

How to Convert Timestamp to Date Type with Carbon

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

In this article, I will explain how to use Carbon, which is commonly used for manipulating date-related data in Laravel, to convert from Timestamp type to Date type.

What is Carbon

Carbon is a convenient package that comes with Laravel, used for converting various date-related data.

Probably most people who use Laravel will end up using it, as it is a basic package in PHP.

Conclusion

PostController.php

// Import the Carbon library
use Carbon\Carbon;
~~~~~~~

$timestamp = '2021-04-29 07:09:50';
$date = Carbon::parse($timestamp)->toDateString();
dd($date);//2021-04-29
'2021-04-29 07:09:50'

This is the format of Timestamp automatically inserted in Laravel when performing actions like save().

For example, when retrieving data like publication dates from YouTube API, it might be outputted as:

2021-04-19T13:14:58Z

This function will neatly convert the output to just the date (date type).

Summary

How was it?

Although very simple, the method for converting Timestamp to Date type is explained above.

I made a note of it because it is easy to forget.

That's all for now.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!