Example of carbon package usage in Laravel

Source: Internet
Author: User
Tags comparison current time datetime


When writing PHP applications, you often need to deal with dates and times, and this article takes you through the API extensions that carbon– inherits from the PHP DateTime class, which makes processing dates and times simpler. The default time-processing class used in Laravel is Carbon.


<?php
namespace Carbon;

Class Carbon extends \datetime
{
code here
}
You can see the code snippet declared above in the Carbon class of the Carbon namespace.

Installation
You can install Carbon by Composer:

Composer require Nesbot/carbon
PS: Because the Laravel project has installed this package by default, you do not need to perform the above command again.

Use
You need to use namespaces to import Carbon, without having to provide the full name each time.

Use Carbon\carbon;
Get current time
You can get the current date and time with the now () method. If you do not specify a parameter, it will use the time zone in the PHP configuration:

<?php
Echo Carbon::now (); 2016-10-14 20:21:20
?>
If you want to use a different time zone, you need to pass a valid time zone as an argument:

Using strings directly
echo carbon::now (' Europe/london '); 2016-10-14 20:21:20
Or
Echo Carbon::now (New Datetimezone (' Europe/london '));
In addition to now (), static functions such as today (), Tomorrow (), yesterday () are provided, but their time is 00:00:00:

Echo carbon::now ();                             //2016-10-14 15:18:34
Echo Carbon::today ();                            //2016-10-14 00:00:00
echo  Carbon::tomorrow (' Europe/london ');                          //2016-10-14 00:00:00
Echo Carbon::yesterday ();                         //2016-10-14 00:00:00
The above output is actually a Carbon type date-time object:

Carbon {#179?
+ "date": "2016-06-14 00:00:00.000000"
+ "Timezone_type": 3
+ "timezone": "UTC"
}
To get the date of the string type, you can use the following code:

Echo Carbon::today ()->todatetimestring ();
Echo Carbon::yesterday ()->todatetimestring ();
Echo Carbon::tomorrow ()->todatetimestring ();
Date type to String
As mentioned above, by default, the Carbon method returns a date-time object. Although it is an object, you can directly use echo to output the result because there is a __tostring magic method. But if you want to convert it to a string, you can use the toDateString or Todatetimestring method:

Echo Carbon::now ()->todatestring (); 2016-10-14
Echo Carbon::now ()->todatetimestring (); 2016-10-14 20:22:50
Date resolution
You can also use the Parse method to resolve any order and type of date (the result is the Carbon type of date-time object):

echo Carbon::p arse (' 2016-10-15 ')->todatetimestring (); 2016-10-15 00:00:00
echo Carbon::p arse (' 2016-10-15 ')->todatetimestring (); 2016-10-15 00:00:00
echo Carbon::p arse (' 2016-10-15 00:10:25 ')->todatetimestring (); 2016-10-15 00:10:25

echo Carbon::p arse (' Today ')->todatetimestring (); 2016-10-15 00:00:00
echo Carbon::p arse (' Yesterday ')->todatetimestring (); 2016-10-14 00:00:00
echo Carbon::p arse (' Tomorrow ')->todatetimestring (); 2016-10-16 00:00:00
echo Carbon::p arse (' 2 days ago ')->todatetimestring (); 2016-10-13 20:49:53
echo Carbon::p arse (' +3 days ')->todatetimestring (); 2016-10-18 20:49:53
echo Carbon::p arse (' +2 weeks ')->todatetimestring (); 2016-10-29 20:49:53
echo Carbon::p arse (' +4 months ')->todatetimestring (); 2017-02-15 20:49:53
echo Carbon::p arse ('-1 Year ')->todatetimestring (); 2015-10-15 20:49:53
echo Carbon::p arse (' next Wednesday ')->todatetimestring (); 2016-10-19 00:00:00
echo Carbon::p arse (' last Friday ')->todatetimestring (); 2016-10-14 00:00:00
Build Date
You can also use a separate calendar to construct the date:

$year = ' 2015 ';
$month = ' 04 ';
$day = ' 12 ';

Echo carbon::createfromdate ($year, $month, $day); 2015-04-12 20:55:59

$hour = ' 02 ';
$minute = ' 15 ':
$second = ' 30 ';

Echo carbon::create ($year, $month, $day, $hour, $minute, $second); 2015-04-12 02:15:30

echo carbon::createfromdate (NULL, 12, 25); Year defaults to the current year
In addition, you can pass a valid time zone as the last parameter.

Date action
Date operations can be done by adding (adding) or sub (minus) to the units to be added or subtracted. For example, if you want to add a date to a specified number of days, you can use the AddDays method. In addition, a modify method is provided, with a parameter format of + or-following values and units. So if you want to add a year to the current date, you can pass +1 years:

Echo Carbon::now ()->adddays (25); 2016-11-09 14:00:01
Echo Carbon::now ()->addweeks (3); 2016-11-05 14:00:01
Echo Carbon::now ()->addhours (25); 2016-10-16 15:00:01
Echo Carbon::now ()->subhours (2); 2016-10-15 12:00:01
Echo Carbon::now ()->addhours (2)->addminutes (12); 2016-10-15 16:12:01
Echo Carbon::now ()->modify (' +15 days '); 2016-10-30 14:00:01
Echo Carbon::now ()->modify ('-2 days '); 2016-10-13 14:00:01
Date comparison
In Carbon you can compare dates by using the following methods:

min– returns the minimum date.
max– returns the maximum date.
eq– judge whether two dates are equal.
gt– determine whether the first date is larger than the second date.
lt– determines whether the first date is smaller than the second date.
gte– determines whether the first date is greater than or equal to the second date.
lte– determines whether the first date is less than or equal to the second date.
Echo Carbon::now ()->tzname; America/toronto
$first = Carbon::create (2012, 9, 5, 23, 26, 11);
$second = Carbon::create (9, 5, NUM, america/vancouver);

echo $first->todatetimestring (); 2012-09-05 23:26:11
Echo $first->tzname; America/toronto
echo $second->todatetimestring (); 2012-09-05 20:26:11
Echo $second->tzname; America/vancouver

Var_dump ($first->eq ($second)); BOOL (TRUE)
Var_dump ($first->ne ($second)); BOOL (FALSE)
Var_dump ($first->gt ($second)); BOOL (FALSE)
Var_dump ($first->gte ($second)); BOOL (TRUE)
Var_dump ($first->lt ($second)); BOOL (FALSE)
Var_dump ($first->lte ($second)); BOOL (TRUE)

$first->setdatetime (2012, 1, 1, 0, 0, 0);
$second->setdatetime (2012, 1, 1, 0, 0, 0); Remember TZ is ' america/vancouver '

Var_dump ($first->eq ($second)); BOOL (FALSE)
Var_dump ($first->ne ($second)); BOOL (TRUE)
Var_dump ($first->gt ($second)); BOOL (FALSE)
Var_dump ($first->gte ($second)); BOOL (FALSE)
Var_dump ($first->lt ($second)); BOOL (TRUE)
Var_dump ($first->lte ($second)); BOOL (TRUE)
To determine whether a date is between two dates, you can use the between () method, and the third optional parameter specifies whether the comparison can be equal, and the default is true:

$first = Carbon::create (2012, 9, 5, 1);
$second = Carbon::create (2012, 9, 5, 5);
Var_dump (Carbon::create, 9, 5, 3)->between ($first, $second)); BOOL (TRUE)
Var_dump (Carbon::create, 9, 5, 5)->between ($first, $second)); BOOL (TRUE)
Var_dump (Carbon::create, 9, 5, 5)->between ($first, $second, false)); BOOL (FALSE)
There are also some helper methods that you can learn from their names:

$dt = Carbon::now ();
 
$dt->isweekday ();
$dt->isweekend ();
$dt->isyesterday ();
$dt->istoday ();
$dt->istomorrow ();
$dt->isfuture ();
$dt->ispast ();
$dt->isleapyear ();
$dt->issameday (Carbon::now ());
$born = Carbon::createfromdate (1987, 4, 23);
$noCake = Carbon::createfromdate (2014, 9, 26);
$yesCake = Carbon::createfromdate (2014, 4, 23);
$overTheHill = Carbon::now ()->subyears (50);
Var_dump ($born->isbirthday ($noCake));              //bool (FALSE)
Var_dump ($born->isbirthday ($yesCake));             //BOOL (TRUE)
Var_dump ($overTheHill->isbirthday ());              //BOOL (TRUE)-> default compare it to today!
Diffforhumans
"One months ago" is easier to read than 30 days ago, and many date libraries offer this common feature, and after the date is parsed, there are four possibilities:

When the comparison time exceeds the current default time
1 days ago
May ago
When comparing the current default time with the future time
1 hours from now
May from now
When the value of the comparison exceeds another value
1 hours ago
May ago
When the value of the comparison is after another value
1 hours later
After May
You can set the second argument to true to remove the modifiers: Diffforhumans (Carbon $other, True).


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

Echo Carbon::now ()->diffforhumans (Carbon::now ()->subyear ()); 1 years later

$dt = Carbon::createfromdate (2011, 8, 1);

echo $dt->diffforhumans ($dt->copy ())->addmonth ()); January ago
echo $dt->diffforhumans ($dt->copy ())->submonth ()); After November

Echo Carbon::now ()->addseconds (5)->diffforhumans (); 5 Seconds from now.

Echo Carbon::now ()->subdays ()->diffforhumans (); 3 weeks ago
Echo Carbon::now ()->subdays ()->diffforhumans (null, TRUE); 3 weeks

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.