Seven very useful carbon methods in Laravel

Source: Internet
Author: User
In writing PHP applications often need to deal with the date and time, carbon inherited from the PHP DateTime class API extension, which makes the processing date and time more simple, this article mainly for you to share the Laravel seven very useful but very few people know carbon method, You need a friend to refer to below.

Objective

As you all know, we use Carbon to handle dates and times in many cases, right? But how many people have read the document thoroughly to see how it has the means? In addition to the well-known now () and format () methods, Carbon has many other useful methods.

Let's take a look here.

1. Isx:true/false

There are many ways to determine whether Carbon objects are today, weekends, leap years, and so on, which are listed in the official documentation:


<?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 if a date is a person's birthday. In addition to the single-check month and day, you can also do this:


$born = Carbon::createfromdate (1987, 4,); $noCake = Carbon::createfromdate (9, +); $yesCake = Carbon:: Createfromdate, 4, Var_dump ($born->isbirthday ($noCake));  BOOL (FALSE) Var_dump ($born->isbirthday ($yesCake));

3. List of STARTOFX and ENDOFX

Below are the STARTOFX series and ENDOFX series:


$dt = carbon::create (1, 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 that do not need to call now (), then replace the seconds and minutes, 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 easy for people to read:


Echo Carbon::now ()->subdays (5)->diffforhumans (); 5 days ago

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


Carbon::setlocale (' zh '); echo Carbon::now ()->addyear ()->diffforhumans (); A year ago

6. Change now () for any time you want


$knownDate = Carbon::create (2001, 5, 21, 12); Create test Date Carbon::settestnow ($knownDate);      Set the mock echo carbon::now ();         2001-05-21 12:00:00

7. Week constants

The following constants can be used instead of 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 list the methods that you find useful in the message type.

Summarize

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.