Share the date and time processing tool (Carbon) instance in PHP-php Tutorial

Source: Internet
Author: User
This article mainly introduces the date and time processing tool (Carbon) in PHP, which has a certain reference value, if you are interested, please take a look at this article. This article mainly introduces the date and time processing tool instance (Carbon) in PHP, which has some reference value. if you are interested, please take a look at it.

Carbon introduction

Carbon is a user-friendly time and date processing plug-in PHP. github has nearly 5000 stars.

Github: https://github.com/briannesbitt/Carbon

Carbon Basic usage

// 1. basic application $ now = Carbon: now (); // 14:13:16 $ today = Carbon: today (); // 00:00:00 $ tomorrow = Carbon :: tomorrow (); // 00:00:00 $ yesterday = Carbon: yesterday (); // 00:00:00 // 2. determine if it is a certain day (Thursday) $ now = Carbon: now (); var_dump ($ now-> isWeekend (); // false because Thursday is not a weekend var_dump ($ now-> isWeekday ()); // true because Thursday is the workday var_dump ($ now-> isThursday (); // true because today is the Thursday $ now-> isToday (); $ now-> isTomorrow (); $ now-> isFuture (); $ now-> isPast (); // 3. create a carbon object for a certain day and perform addition and subtraction calculation $ date = Carbon: create (2016, 12, 25, 0, 0, 0 ); // 00:00:00 $ next_year = $ date-> addYears (2); // 00:00:00 $ past_year = $ date-> subYears (2 ); // 00:00:00 $ next_month = $ date-> addMonths (2); // 00:00:00 $ past_month = $ date-> subMonths (2 ); // 00:00:00 $ next_day = $ date-> addDays (2); // 00:00:00 $ past_day = $ date-> subDays (2); // 00:00:00... addWeekdays (), addWeeks (), addHours (), and other methods // 4. convert the carbon object to the string type $ dt = Carbon: create (1975, 12, 25, 14, 15, 16); echo $ dt-> toDateString (); // 1975-12-25echo $ dt-> toFormattedDateString (); // Dec 25,197 5 echo $ dt-> toTimeString (); // 14: 15: 16 echo $ dt-> toDateTimeString (); // 1975-12-25 14: 15: 16 echo $ dt-> toDayDateTimeString (); // Thu, Dec 25,197

The above describes some basic Carbon usage. Carbon is flexible and user-friendly.

The above is a detailed description of the Carbon instance in PHP. For more information, see other related articles in the first PHP community!

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.