The ability to create a time library with 8000+ stars on Github and built-in adoption in various PHP frameworks all demonstrate Carbon’s excellence.
This tutorial uses carbon version 1.25.0 and runs in the following environment:
- PHP7.1.8 (Carbon minimum version is 5.3)
- Yii2.0.14 (Not required as a carbon demo)
- Nginx
Install it
Carbon supports manual installation and composer installation.
Composer installation
This is the simplest and can be done with the following code.
composer require nesbot/carbon
Copy the code
This extension comes with two downloadable extension packs, which can be found in the Vendor directory.
Manual installation
I don’t recommend doing this, but if you must, follow these steps
1. Download the carbon.php file anywhere in your application.
https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php
2. Start using the carbon-.php file we just downloaded.
require 'path/to/Carbon.php';
use Carbon\Carbon;
echo Carbon::now();
Copy the code
Begin to use
Carbon is more semantic when we manipulate time, it provides some object methods and some static methods, we will use the question and answer format for ease of explanation.
Get the current time
In PHP we can construct it using the date method and enter the format we want, whereas in Carbon it looks like this
Carbon::now();/ / the 2018-03-27 21:52:45
Carbon::now('Europe/London');You can also specify a time zone
Copy the code
You can also easily get time zones
Carbon::now()->tzName;// Asia/Shanghai
Copy the code
With now(), Natural Carbon also nicely supports the following static methods
Carbon::today();/ / 2018-03-27 00:00:00
Carbon::tomorrow();/ / 2018-03-28 00:00:00
Carbon:: yesterday();/ / 2018-03-26 00:00:00
Copy the code
As with now(), you can pass in a time zone argument.
At some point in time
We can also add details to methods like now() above, such as I want to get the value of yesterday’s 8:00 a.m.
Carbon::yesterday()->addHours(8);/ / the 2018-03-26 08:00:00
Carbon::yesterday()->addHours(8)->addMinutes(29);/ / the 2018-03-26 08:29:00
Carbon::yesterday()->addHours(8)->addMinutes(29)->addSeconds(19);/ / the 2018-03-26 08:29:19
Copy the code
As phper, we like to convert the time into a timestamp and store it in a database. Carbon is easy to use
Carbon::yesterday()->addHours(8)->timestamp;
Copy the code
Easy access to time stamps.
Is this the future or the past?
In this world, there are many days worth remembering, such as birthdays, such as Saturdays, such as the future and so on. With this in mind, Carbon devised the following method.
- isWeekday
- isWeekend
- isYesterday
- isToday
- isTomorrow
- isNextWeek
- isLastWeek
- isNextMonth
- isLastMonth
- isNextYear
- isLastYear
- isFuture
- isPast
- isLeapYear
- isLongYear
- isSameAs
- isCurrentYear
- isSameYear
- isCurrentMonth
- isSameMonth
- isSameDay
- isDayOfWeek
- isSunday
- isMonday
- isTuesday
- isWednesday
- isThursday
- isFriday
- isSaturday
Most of them are understandable, with some special instructions
- IsFuture of the future
- IsPast past
- IsLeapYear leap year
All rivers run into sea
We just generated Carbon objects from moments such as now, Today, etc. In addition to these Carbon objects, we can also contain other sources such as the ones below
Carbon::createFromDate($year, $month, $day, $tz);
Carbon::createFromTime($hour, $minute, $second, $tz);
Carbon::createFromTimeString("$hour:$minute:$second", $tz);
Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
Copy the code
$tz indicates the time zone.
There’s another one we always like: get the time from the timestamp.
Carbon::createFromTimestamp(time())->addHours(- 1);// Get the point in time one hour ago
Copy the code
Notice that for methods like addHours, you can put in the plural before, and see if it’s the same as what we think it is.
Parsing time
There are other ways to get the time, and there is another way to get the Carbon object by parsing the passed time and further manipulating it, for example
Carbon::parse("2018-03-27")->addHours(- 1);/ / the 2018-03-26 23:00:00
Copy the code
First Day of January 2008 is also supported in some languages
Carbon::parse("first day of January 2008")->addHours(- 1);
Copy the code
Isn’t that smart?
Three seconds ago
Yii::$app->formatter->asRelativeTime()
Carbon::now()->diffForHumans();// 1 second ago
Copy the code
Ah, how or English that? Fear not, we support local language packs as follows.
Carbon::setLocale('zh');
Carbon::now()->diffForHumans();/ / 1 seconds ago
Copy the code
Carbon is available in 64 languages to date.
There are also some parameter configurations supported for diffForHumans, which we describe in the Carbon lookup table.
formatting
Carbon allows us to have different formats and different outputs for a given time, as shown in the following code
$dt = Carbon::create(1975.12.25.14.15.16);
echo $dt->toDateString(); / / 1975-12-25
echo $dt->toFormattedDateString(); // Dec 25, 1975
echo $dt->toTimeString(); / / 14:15:16
echo $dt->toDateTimeString(); / / the 1975-12-25 14:15:16
echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
Copy the code
And Carbon provides a universal approach
$dt->format("Y-m-d H:i:s")
Copy the code
Judge whether it fits (key points)
Sometimes we need to determine if the user is entering a value in the format we want. We can use the hasFormat method provided by Carbon.
$dt->hasFormat('2018-03-03'.'Y-m-d');//false
Copy the code
Return true and false.
Interval judgment
Sometimes we need to determine whether the user’s input time is within the specified time range, which can be easily done with Carbon.
Carbon::parse($date)->between($first, $second);// true / false
Copy the code
Notice hereSecond is also a Carbon object.
summary
The directory structure of this library is very simple, but there are many different ways to use Carbon.
In addition, DURING this period of time, I will find time to sort out Carbon’s quick list, and you can check it with one click after it is released.
A link to the
- Official address http://carbon.nesbot.com
Abei’s knowledge sharing https://nai8.me