Carbon Date Time processing library can be very convenient processing time, GitHub address is https://github.com/briannesbitt/carbon, this article mainly introduces the detailed PHP use date time processor Carbon humanized display time, has certain reference value, has the interest can understand, hoped can help to everybody.
Can be easily installed through the Composer Carbon
# Composer require Nesbot/carbon
The use of the method is also very simple
<?phprequire ' vendor/autoload.php '; use carbon\carbon;//display Chinese carbon::setlocale (' zh ');//Get yesterday's timestamp $ts = Carbon:: Yesterday ()->timestamp;//humanized display time echo carbon::createfromtimestamp ($ts)->diffforhumans ();
The above print results are 1 days ago
How to use in the Laravel framework
First, in order to display the Chinese, app/Providers/AppServiceProvider.php
add \Carbon\Carbon::setLocale('zh');
to the method in boot()
, as follows:
Public Function boot () { \carbon\carbon::setlocale (' zh ');}
Then you can use, for example, in a method in Articlecontroller display the article published date, if the publication date is timestamp, in the head reference carbon, add the following code
Use Carbon\carbon;
humanized publication Time
Carbon::createfromtimestamp ($published _at)->diffforhumans ();
Carbon In addition to the user-friendly display time has a lot of processing time function, the specific use of the method please refer to the official documents.