PHP using date-time Processor Carbon instance method

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.