The seven Carbon Methods in Laravel are very useful but rarely known. laravelcarbon

Source: Internet
Author: User

The seven Carbon Methods in Laravel are very useful but rarely known. laravelcarbon

Preface

Everyone should know that we often use Carbon to process dates and times, right? But how many people have read the document to understand how it works? In addition to the well-known now () and format () methods, Carbon has many other useful methods.

Let's take a look.

1. isX: True/False

There are many ways to determine whether the Carbon object is today, weekend, leap year, and so on. These are listed in the official documents below:

<?php$dt->isWeekday();$dt->isWeekend();$dt->isYesterday();$dt->isToday();$dt->isTomorrow();$dt->isFuture();$dt->isPast();$dt->isLeapYear();$dt->isSameDay(Carbon::now());

2. isBirthday

In addition to the list above, Carbon also has a way to determine whether a date is a person's birthday. In addition to the single-open verification month and day, you can also do this:

$born = Carbon::createFromDate(1987, 4, 23);$noCake = Carbon::createFromDate(2014, 9, 26);$yesCake = Carbon::createFromDate(2014, 4, 23);var_dump($born->isBirthday($noCake));  // bool(false)var_dump($born->isBirthday($yesCake));

3. StartOfX and EndOfX list

The startOfX series and endOfX series are as follows:

$dt = Carbon::create(2012, 1, 31, 12, 0, 0);echo $dt->startOfDay(); // 2012-01-31 00:00:00echo $dt->endOfDay(); // 2012-01-31 23:59:59echo $dt->startOfMonth(); // 2012-01-01 00:00:00echo $dt->endOfMonth(); // 2012-01-31 23:59:59echo $dt->startOfYear(); // 2012-01-01 00:00:00echo $dt->endOfYear(); // 2012-12-31 23:59:59echo $dt->startOfDecade(); // 2010-01-01 00:00:00echo $dt->endOfDecade(); // 2019-12-31 23:59:59echo $dt->startOfCentury(); // 2000-01-01 00:00:00echo $dt->endOfCentury(); // 2099-12-31 23:59:59echo $dt->startOfWeek(); // 2012-01-30 00:00:00echo $dt->endOfWeek(); // 2012-02-05 23:59:59

4. Today, Tomorrow, Yesterday

Three simple but very useful methods: Do not call now (), replace the hour, minute, and second, and then increase or subtract the number of days:

$today = Carbon::today(); // assuming 2016-06-24echo $today;        // 2016-06-24 00:00:00$tomorrow = Carbon::tomorrow();echo $tomorrow;       // 2016-06-25 00:00:00$yesterday = Carbon::yesterday();echo $yesterday;       // 2016-06-23 00:00:00

5. DiffForHumans + Localization

You may have used this method called diffForHumans ()-It returns the difference between two dates in a way that is easier for people to read:

echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago

But do you know it can also be localized? You only need to change the location, such as Chinese:

Carbon: setLocale ('zh '); echo Carbon: now ()-> addYear ()-> diffForHumans (); // a year ago

6. Change now () to any time you want

$ KnownDate = Carbon: create (2001, 5, 21, 12); // create the test date Carbon: setTestNow ($ knownDate); // set the mock echo Carbon :: now (); // 12:00:00

7. Week Constants

The following constant can be used to replace the day of the week:

var_dump(Carbon::SUNDAY);       // int(0)var_dump(Carbon::MONDAY);       // int(1)var_dump(Carbon::TUESDAY);       // int(2)var_dump(Carbon::WEDNESDAY);      // int(3)var_dump(Carbon::THURSDAY);      // int(4)var_dump(Carbon::FRIDAY);       // int(5)var_dump(Carbon::SATURDAY);      // int(6)

Do you know all the useful methods mentioned above? You can enter a message to list the methods that you find useful.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.