Carbon - Gives yii2 powerful time manipulation capabilities

Being able to make a time library with 8000+ stars on github and built-in adoption of major PHP frameworks all reflect the extraordinary qualities of Carbon.

Abei will try to use the most common language to get you started. This tutorial uses carbon version 1.25.0, and the operating environment is as follows:

  • PHP7.1.8 (carbon minimum version requirement is 5.3)
  • Yii2.0.14 (as a carbon demo program, not required)
  • Nginx

install it

Carbon supports manual installation and composer installation.

composer install

This is the easiest and can be done with the following code.

composer require nesbot/carbon

This extension will have two extension packs downloaded, which can be found in the vendor directory.

Enter image description

Manual installation

I do not recommend doing this, if it is absolutely necessary, please follow the steps below

1. Download the Carbon.php file to any location in your program.

https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php

2. Getting started Import the Carbon.php file we just downloaded.

require 'path/to/Carbon.php';

use Carbon\Carbon;
echo Carbon::now();

start using

Carbon is more semantic when we operate time. It provides some object methods and some static methods. For the convenience of explanation, we use question and answer style.

get current time

In php, we can construct through the date method and enter the format we want, while in Carbon it is as follows

Carbon::now();//2018-03-27 21:52:45
Carbon::now('Europe/London');//你也可以指定时区

Of course you can also easily get the time zone

Carbon::now()->tzName;// Asia/Shanghai

Since there is now(), Carbon also supports the following static methods very intimately

Carbon::today();// 2018-03-27 00:00:00
Carbon::tomorrow();//2018-03-28 00:00:00
Carbon:: yesterday();//2018-03-26 00:00:00

Like now(), you can pass it a timezone parameter.

some point in time

We can also add details to methods like now() above, say I want to get the time value of 8am yesterday morning.

Carbon::yesterday()->addHours(8);//2018-03-26 08:00:00
Carbon::yesterday()->addHours(8)->addMinutes(29);//2018-03-26 08:29:00
Carbon::yesterday()->addHours(8)->addMinutes(29)->addSeconds(19);//2018-03-26 08:29:19

As a phper, we always like to convert the time into a timestamp and store it in the database. It is very simple to use Carbon

Carbon::yesterday()->addHours(8)->timestamp;

Easily get timestamps.

Is this moment the future or the past?

There are many days in this world that are worth remembering, such as birthdays, such as Saturdays, such as the future, etc. Considering such needs, Carbon has planned the following methods.

  • 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 can be understood, some special instructions

  • isFuture future
  • isPast past
  • isLeapYear leap year

All rivers are inclusive

We just generated Carbon objects through some moments, such as now, today, etc. In addition to these Carbon can also contain other sources, such as the following

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);

$tz stands for time zone.

There's also one of our old favorites, getting the time from a timestamp.

Carbon::createFromTimestamp(time())->addHours(-1);//获得一个小时前的时间点

Note that for methods such as addHours, before you can fill in the plural representation, is it the same as what we thought in our minds.

parsing time

The above are all obtained by other forms of time, and another is to obtain the Carbon object by parsing the passed time and then perform further operations, such as

Carbon::parse("2018-03-27")->addHours(-1);// 2018-03-26 23:00:00

Also supports some language content first day of January 2008

Carbon::parse("first day of January 2008")->addHours(-1);

is not very smart.

3 seconds ago

Another commonly used time format is 5 hours ago and 4 seconds ago. Of course, there is Yii::$app->formatter->asRelativeTime() in yii2 to achieve this purpose. How about using Carbon?

Carbon::now()->diffForHumans();// 1 second ago

Yeah, why is it still in English? Don't be afraid, we support local language packs, as follows.

Carbon::setLocale('zh');
Carbon::now()->diffForHumans();// 1秒前

So far, Carbon supports a total of 64 languages.

Some parameter configurations are also supported for diffForHumans, which we will give in the Carbon quick reference table.

format

For a time, Carbon allows us to format and output differently, such as 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();                      // 1975-12-25 14:15:16
echo $dt->toDayDateTimeString();                   // Thu, Dec 25, 1975 2:15 PM

And Carbon also provides a generic method

$dt->format("Y-m-d H:i:s")

Judging whether it is in line with (emphasis)

Sometimes we need to judge whether the value input by the user is the time format we want, we can use the hasFormat method provided by Carbon.

$dt->hasFormat('2018-03-03', 'Y-m-d');//false

Returns true and false.

Interval judgment

Sometimes we need to judge whether the time entered by the user is within the specified time range, which can be done easily with Carbon.

Carbon::parse($date)->between($first, $second);// true / false

Note here that $first and $second are also Carbon objects.

summary

The above are some common methods of Carbon. Of course, there are many less commonly used methods. For details, you can go to the source file Carbon.php to see. The directory structure of this library is very simple, that is, there are many methods.

In addition, during this time, I will take the time to sort out Carbon's cheat sheet, and you can quickly check it with one click after it is released.

Related Links

A Bei's knowledge sharing https://nai8.me

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061661&siteId=291194637