1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php carbon 中文 Carbon中文使用手册(下)

php carbon 中文 Carbon中文使用手册(下)

时间:2019-11-22 08:20:33

相关推荐

php carbon 中文 Carbon中文使用手册(下)

·Serialization

Carbon实例能被序列化的。

$dt=Carbon::create(,12,25,20,30,00,'Europe/Moscow');

echoserialize($dt);//O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"-12-2520:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";}

//等同于:

echo$dt->serialize();//O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"-12-2520:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";}

$dt='O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"-12-2520:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";}';

echounserialize($dt)->format('Y-m-d\TH:i:s.uPT');//-12-25T20:30:00.000000+04:00MSK

//等同于:

echoCarbon::fromSerialized($dt)->format('Y-m-d\TH:i:s.uPT');//-12-25T20:30:00.000000+04:00MSK

·JSON

Carbon实例可以从JSON编码和解码(这些特性只能从PHP 5.4+中获得,参见下面关于PHP 5.3的注释)。

$dt=Carbon::create(,12,25,20,30,00,'Europe/Moscow');

echojson_encode($dt);

//{"date":"-12-2520:30:00.000000","timezone_type":3,"timezone":"Europe\/Moscow"}

$json='{"date":"-12-2520:30:00.000000","timezone_type":3,"timezone":"Europe\/Moscow"}';

$dt=Carbon::__set_state(json_decode($json,true));

echo$dt->format('Y-m-d\TH:i:s.uPT');

//-12-25T20:30:00.000000+04:00MSK

您可以使用serializeUsing()自定义序列化。

$dt=Carbon::create(,12,25,20,30,00,'Europe/Moscow');

Carbon::serializeUsing(function($date){

return$date->getTimestamp();

});

echojson_encode($dt);

/*

1356453000

*/

//CallserializeUsingwithnulltoresettheserializer:

Carbon::serializeUsing(null);

jsonSerialize()方法返回中间通过“json_encode”将其转换为字符串,它还允许您使用PHP 5.3兼容性。

$dt=Carbon::create(,12,25,20,30,00,'Europe/Moscow');

echojson_encode($dt->jsonSerialize());

//{"date":"-12-2520:30:00.000000","timezone_type":3,"timezone":"Europe\/Moscow"}

//Thisisequivalenttothefirstjson_encodeexamplebutworkswithPHP5.3.

//Anditcanbeusedseparately:

var_dump($dt->jsonSerialize());

//array(3){

["date"]=>

string(26)"-12-2520:30:00.000000"

["timezone_type"]=>

int(3)

["timezone"]=>

string(13)"Europe/Moscow"

}

·Macro

如果您习惯于使用Laravel和对象(如响应或集合),您可能熟悉这个宏概念。Carbon macro()的工作方式与Laravel宏特性相同,它将方法名作为第一个参数,闭包作为第二个参数。这使得闭包操作可以作为一个具有给定名称的方法在所有Carbon实例(也可以作为Carbon static方法)上使用。

在PHP 5.4中,$this可用于闭包中引用当前实例。对于PHP 5.3的兼容性,我们还向闭包添加了一个“$self”属性。例子:

Carbon::macro('diffFromYear',function($year,$self=null){

//这个块是为了在独立的Carbon上与PHP版本

if(!isset($self)&&isset($this)){

$self=$this;

}

//兼容性块的结束。

return$self->diffForHumans(Carbon::create($year,1,1,0,0,0),false,false,3);

});

echoCarbon::parse('-01-1212:00:00')->diffFromYear();//1year1week4daysafter

兼容性块允许您确保宏的完全兼容性。一个关于Illuminate\Support\Carbon (Laravel包装类)的宏将不会被定义,正如上面在PHP 5.3 $this中提到的,这个不会被定义。要使宏在任何地方都能工作,只需粘贴这个if语句测试如果它是定义的,而不是$self然后复制它,然后在函数体中使用$self。

不管您是否省略了一些可选参数,只要$self有这个名称,并且是最后一个参数:

Carbon::macro('diffFromYear',function($year,$absolute=false,$short=false,$parts=1,$self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

return$self->diffForHumans(Carbon::create($year,1,1,0,0,0),$absolute,$short,$parts);

});

echoCarbon::parse('-01-1212:00:00')->diffFromYear();//1yearafter

echoCarbon::parse('-01-1212:00:00')->diffFromYear(,true);//1year

echoCarbon::parse('-01-1212:00:00')->diffFromYear(,true,true);//1yr

echoCarbon::parse('-01-1212:00:00')->diffFromYear(,true,true,5);//1yr1w4d12h

还可以将宏分组到类中,并与mixin()一起应用

ClassBeerDayCarbonMixin

{

publicfunctionnextBeerDay()

{

returnfunction($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

return$self->modify('nextwednesday');

};

}

publicfunctionpreviousBeerDay()

{

returnfunction($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

return$self->modify('previouswednesday');

};

}

}

Carbon::mixin(newBeerDayCarbonMixin());

$date=Carbon::parse('FirstsaturdayofDecember');

echo$date->previousBeerDay();//-11-2800:00:00

echo$date->nextBeerDay();//-12-0500:00:00

您可以用hasMacro()检查是否可用宏(包括mixin)

var_dump(Carbon::hasMacro('previousBeerDay'));//bool(true)

var_dump(Carbon::hasMacro('diffFromYear'));//bool(true)

var_dump(Carbon::hasMacro('dontKnowWhat'));//bool(false)

你猜怎么着?在CarbonInterval和CarbonPeriod类上也可以使用所有的宏方法。

CarbonInterval::macro('twice',function($self=null){

return$self->times(2);

});

echoCarbonInterval::day()->twice()->forHumans();//2days

echoCarbonInterval::hours(2)->minutes(15)->twice()->forHumans(true);//4h30m

CarbonPeriod::macro('countWeekdays',function($self=null){

return$self->filter('isWeekday')->count();

});

echoCarbonPeriod::create('-11-01','-11-30')->countWeekdays();//22

echoCarbonPeriod::create('-12-01','-12-31')->countWeekdays();//21

以下是社区提出的一些有用的宏:

Carbon::macro('isHoliday',function($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

returnin_array($self->format('d/m'),[

'25/12',//Christmas

'01/01',//NewYear

//...

]);

});

var_dump(Carbon::createMidnightDate(,12,25)->isHoliday());//bool(true)

var_dump(Carbon::createMidnightDate(,6,25)->isHoliday());//bool(false)

var_dump(Carbon::createMidnightDate(,1,1)->isHoliday());//bool(true)

Credit:kylekatarnls(# 116)。

检查cmixin/业务日以获得更完整的业务日处理程序。

ClassCurrentDaysCarbonMixin

{

/**

*Getthealldatesofweek

*

*@returnarray

*/

publicstaticfunctiongetCurrentWeekDays()

{

returnfunction($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

$startOfWeek=($self?:static::now())->startOfWeek()->subDay();

$weekDays=array();

for($i=0;$i

$weekDays[]=$startOfWeek->addDay()->startOfDay()->copy();

}

return$weekDays;

};

}

/**

*Getthealldatesofmonth

*

*@returnarray

*/

publicstaticfunctiongetCurrentMonthDays()

{

returnfunction($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

$startOfMonth=($self?:static::now())->startOfMonth()->subDay();

$endOfMonth=($self?:static::now())->endOfMonth()->format('d');

$monthDays=array();

for($i=0;$i

{

$monthDays[]=$startOfMonth->addDay()->startOfDay()->copy();

}

return$monthDays;

};

}

}

Carbon::mixin(newCurrentDaysCarbonMixin());

functiondumpDateList($dates){

echosubstr(implode(',',$dates),0,100).'...';

}

dumpDateList(Carbon::getCurrentWeekDays());//-07-0200:00:00,-07-0300:00:00,-07-0400:00:00,-07-0500:00:00,-07-0600:00...

dumpDateList(Carbon::getCurrentMonthDays());//-07-0100:00:00,-07-0200:00:00,-07-0300:00:00,-07-0400:00:00,-07-0500:00...

dumpDateList(Carbon::now()->subMonth()->getCurrentWeekDays());//-06-0400:00:00,-06-0500:00:00,-06-0600:00:00,-06-0700:00:00,-06-0800:00...

dumpDateList(Carbon::now()->subMonth()->getCurrentMonthDays());//-06-0100:00:00,-06-0200:00:00,-06-0300:00:00,-06-0400:00:00,-06-0500:00...

Carbon::macro('toAtomStringWithNoTimezone',function($self=null){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

return$self->format('Y-m-d\TH:i:s');

});

echoCarbon::parse('-06-1620:08:34')->toAtomStringWithNoTimezone();//-06-16T20:08:34

Carbon::macro('easterDate',function($year){

returnCarbon::createMidnightDate($year,3,21)->addDays(easter_days($year));

});

echoCarbon::easterDate()->format('d/m');//05/04

echoCarbon::easterDate()->format('d/m');//27/03

echoCarbon::easterDate()->format('d/m');//16/04

echoCarbon::easterDate()->format('d/m');//01/04

echoCarbon::easterDate()->format('d/m');//21/04

查看cmixin/工作日以获得更完整的假日处理程序。

Carbon::macro('range',function($startDate,$endDate){

returnnewDatePeriod($startDate,newDateInterval('P1D'),$endDate);

});

foreach(Carbon::range(Carbon::createMidnightDate(,3,28),Carbon::createMidnightDate(,4,3))as$date){

echo"$date\n";

}

/*

-03-2800:00:00

-03-2900:00:00

-03-3000:00:00

-03-3100:00:00

-04-0100:00:00

-04-0200:00:00

*/

classUserTimezoneCarbonMixin

{

public$userTimeZone;

/**

*Setusertimezone,willbeusedbeforeformatfunctiontoapplycurrentusertimezone

*

*@param$timezone

*/

publicfunctionsetUserTimezone()

{

$mixin=$this;

returnfunction($timezone)use($mixin){

$mixin->userTimeZone=$timezone;

};

}

/**

*Returnsdateformattedaccordingtogivenformat.

*

*@paramstring$format

*

*@returnstring

*

*@link/manual/en/datetime.format.php

*/

publicfunctiontzFormat()

{

$mixin=$this;

returnfunction($format,$self=null)use($mixin){

//compatibilitychunk

if(!isset($self)&&isset($this)){

$self=$this;

}

if(!is_null($mixin->userTimeZone)){

$self->timezone($mixin->userTimeZone);

}

return$self->format($format);

};

}

}

Carbon::mixin(newUserTimezoneCarbonMixin());

Carbon::setUserTimezone('Europe/Berlin');

echoCarbon::createFromTime(12,0,0,'UTC')->tzFormat('H:i');//14:00

echoCarbon::createFromTime(15,0,0,'UTC')->tzFormat('H:i');//17:00

Carbon::setUserTimezone('America/Toronto');

echoCarbon::createFromTime(12,0,0,'UTC')->tzFormat('H:i');//08:00

echoCarbon::createFromTime(15,0,0,'UTC')->tzFormat('H:i');//11:00

·CarbonInterval

CarbonInterval类继承了PHP DateInterval类。

classCarbonIntervalextends\DateInterval

{

//codehere

}

你可以通过以下方式创建实例

echoCarbonInterval::year();//1year

echoCarbonInterval::months(3);//3months

echoCarbonInterval::days(3)->seconds(32);//3days32seconds

echoCarbonInterval::weeks(3);//3weeks

echoCarbonInterval::days(23);//3weeks2days

echoCarbonInterval::create(2,0,5,1,1,2,7);//2years5weeks1day1hour2minutes7seconds

如果您发现自己从另一个库继承了\DateInterval实例,不要害怕!您可以通过一个友好的instance()函数创建一个CarbonInterval实例。

$di=new\DateInterval('P1Y2M');//<==instancefromanotherAPI

$ci=CarbonInterval::instance($di);

echoget_class($ci);//'Carbon\CarbonInterval'

echo$ci;//1year2months

其他的帮助程序,但是要注意实现提供了帮助程序来处理几周,但是只节省了几天。数周是根据当前实例的总天数计算的。

echoCarbonInterval::year()->years;//1

echoCarbonInterval::year()->dayz;//0

echoCarbonInterval::days(24)->dayz;//24

echoCarbonInterval::days(24)->daysExcludeWeeks;//3

echoCarbonInterval::weeks(3)->days(14)->weeks;//2

echoCarbonInterval::weeks(3)->weeks;//3

echoCarbonInterval::minutes(3)->weeksAndDays(2,5);//2weeks5days3minutes

CarbonInterval扩展DateInterval,您可以使用ISO-8601的持续时间格式创建这两种格式:

$ci=CarbonInterval::create('P1Y2M3D');

$ci=newCarbonInterval('PT0S');

借助fromString()方法,可以从友好的字符串创建Carbon intervals。

CarbonInterval::fromString('2 minutes 15 seconds');

CarbonInterval::fromString('2m 15s'); // or abbreviated

注意这个月缩写为“mo”以区别于分钟和整个语法不区分大小写。

它还有一个方便的for human(),它被映射为__toString()实现,用于为人类打印间隔。

CarbonInterval::setLocale('fr');

echoCarbonInterval::create(2,1)->forHumans();//2ans1mois

echoCarbonInterval::hour()->seconds(3);//1heure3secondes

CarbonInterval::setLocale('en');

如您所见,您可以使用CarbonInterval::setLocale('fr')更改字符串的语言环境。

至于Carbon,您可以使用make方法从其他区间或字符串返回一个新的CarbonInterval实例:

$dateInterval=newDateInterval('P2D');

$carbonInterval=CarbonInterval::month();

echoCarbonInterval::make($dateInterval)->forHumans();//2days

echoCarbonInterval::make($carbonInterval)->forHumans();//1month

echoCarbonInterval::make('PT3H')->forHumans();//3hours

echoCarbonInterval::make('1h15m')->forHumans();//1hour15minutes

//Passtruetogetshortformat

echoCarbonInterval::make('1h15m')->forHumans(true);//1h15m

本机DateInterval分别添加和相乘,因此:

$interval=CarbonInterval::make('7h55m');

$interval->add(CarbonInterval::make('17h35m'));

$interval->times(3);

echo$interval->forHumans();//72hours270minutes

从单位到单位的输入中得到纯计算。将分钟级联成小时、小时级联成天等。使用级联方法:

echo $interval->forHumans(); // 72 hours 270 minutes

echo $interval->cascade()->forHumans(); // 3 days 4 hours 30 minutes

默认的因素有:

1分钟= 60秒

1小时=60分钟

1天=24小时

1周= 7天

1个月= 4周

1年= 12个月

CarbonIntervals没有上下文,所以它们不能更精确(没有DST、没有闰年、没有实际的月长或年长)。但是你可以完全定制这些因素。例如处理工作时间日志:

$cascades = CarbonInterval::getCascadeFactors(); // save initial factors

CarbonInterval::setCascadeFactors(array(

'minute' => array(60, 'seconds'),

'hour' => array(60, 'minutes'),

'day' => array(8, 'hours'),

'week' => array(5, 'days'),

// in this example the cascade won't go farther than week unit

));

echo CarbonInterval::fromString('20h')->cascade()->forHumans(); // 2 days 4 hours

echo CarbonInterval::fromString('10d')->cascade()->forHumans(); // 2 weeks

echo CarbonInterval::fromString('3w 18d 53h 159m')->cascade()->forHumans(); // 7 weeks 4 days 7 hours 39 minutes

// You can see currently set factors with getFactor:

echo CarbonInterval::getFactor('minutes', /* per */ 'hour'); // 60

echo CarbonInterval::getFactor('days', 'week'); // 5

// And common factors can be get with short-cut methods:

echo CarbonInterval::getDaysPerWeek(); // 5

echo CarbonInterval::getHoursPerDay(); // 8

echo CarbonInterval::getMinutesPerHours(); // 60

echo CarbonInterval::getSecondsPerMinutes(); // 60

CarbonInterval::setCascadeFactors($cascades); // restore original factors

是否可能将间隔转换为给定的单元(使用提供的级联因子)。

echo CarbonInterval::days(3)->hours(5)->total('hours'); // 77

echo CarbonInterval::days(3)->hours(5)->totalHours; // 77

echo CarbonInterval::months(6)->totalWeeks; // 24

echo CarbonInterval::year()->totalDays; // 336

您还可以使用spec()获得inverval的ISO 8601规范

echo CarbonInterval::days(3)->hours(5)->spec(); // P3DT5H

也可以从DateInterval对象获取它,因为它是静态助手:

echo CarbonInterval::getDateIntervalSpec(new DateInterval('P3DT6M10S')); // P3DT6M10S

使用compare()和comparedateinterval()方法可以对日期间隔列表进行排序:

$halfDay = CarbonInterval::hours(12);

$oneDay = CarbonInterval::day();

$twoDay = CarbonInterval::days(2);

echo CarbonInterval::compareDateIntervals($oneDay, $oneDay); // 0

echo $oneDay->compare($oneDay); // 0

echo CarbonInterval::compareDateIntervals($oneDay, $halfDay); // 1

echo $oneDay->compare($halfDay); // 1

echo CarbonInterval::compareDateIntervals($oneDay, $twoDay); // -1

echo $oneDay->compare($twoDay); // -1

$list = array($twoDay, $halfDay, $oneDay);

usort($list, array('Carbon\CarbonInterval', 'compareDateIntervals'));

echo implode(', ', $list); // 12 hours, 1 day, 2 days

最后,通过使用互补参数调用toPeriod(),可以将一个CarbonInterval实例转换为一个CarbonPeriod实例。

我听到你问什么是CarbonPeriod实例。哦!完美过渡到下一章。

·CarbonPeriod

CarbonPeriod是一个友好的DatePeriod版本,具有许多快捷方式。

// Create a new instance:

$period = new CarbonPeriod('-04-21', '3 days', '-04-27');

// Use static constructor:

$period = CarbonPeriod::create('-04-21', '3 days', '-04-27');

// Use the fluent setters:

$period = CarbonPeriod::since('-04-21')->days(3)->until('-04-27');

// Start from a CarbonInterval:

$period = CarbonInterval::days(3)->toPeriod('-04-21', '-04-27');

CarbonPeriod可以通过多种方式构建:

开始日期、结束日期和可选间隔(默认为1天),

起始日期,递归次数和可选区间,

ISO 8601间隔规范。

日期可以是DateTime/Carbon实例,绝对字符串如“-10-15 15:00”或相对字符串,例如“next monday”。Interval可以作为DateInterval/CarbonInterval实例、ISO 8601的Interval规范(如“P4D”)或人类可读字符串(如“4 days”)给出。

默认构造函数和create()方法在参数类型和顺序方面都很容易理解,所以如果您想要更精确,建议使用fluent语法。另一方面,您可以将动态值数组传递给createFromArray(),它将使用给定的数组作为参数列表构造一个新实例。

CarbonPeriod实现迭代器接口。它意味着它可以直接传递给foreach循环:

$period = CarbonPeriod::create('-04-21', '3 days', '-04-27');

foreach ($period as $key => $date) {

if ($key) {

echo ', ';

}

echo $date->format('m-d');

}

// 04-21, 04-24, 04-27

// Here is what happens under the hood:

$period->rewind(); // restart the iteration

while ($period->valid()) { // check if current item is valid

if ($period->key()) { // echo comma if current key is greater than 0

echo ', ';

}

echo $period->current()->format('m-d'); // echo current date

$period->next(); // move to the next item

}

// 04-21, 04-24, 04-27

参数可以在迭代过程中进行修改:

$period = CarbonPeriod::create('-04-29', 7);

$dates = array();

foreach ($period as $key => $date) {

if ($key === 3) {

$period->invert()->start($date); // invert() is an alias for invertDateInterval()

}

$dates[] = $date->format('m-d');

}

echo implode(', ', $dates); // 04-29, 04-30, 05-01, 05-02, 05-01, 04-30, 04-29

和DatePeriod一样,CarbonPeriod也支持ISO 8601时间间隔规范。

请注意,本机日期周期将递归处理为多次重复间隔。因此,在排除开始日期时,它将减少一个结果。CarbonPeriod的自定义过滤器的引入使得知道结果的数量变得更加困难。由于这个原因,我们稍微改变了实现,递归被视为返回日期的总体限制。

// Possible options are: CarbonPeriod::EXCLUDE_START_DATE | CarbonPeriod::EXCLUDE_END_DATE

// Default value is 0 which will have the same effect as when no options are given.

$period = CarbonPeriod::createFromIso('R4/-07-01T00:00:00Z/P7D', CarbonPeriod::EXCLUDE_START_DATE);

$dates = array();

foreach ($period as $date) {

$dates[] = $date->format('m-d');

}

echo implode(', ', $dates); // 07-08, 07-15, 07-22, 07-29

您可以从不同的getter中检索数据:

$period = CarbonPeriod::create('-05-06', '-05-25', CarbonPeriod::EXCLUDE_START_DATE);

$exclude = $period->getOptions() & CarbonPeriod::EXCLUDE_START_DATE;

echo $period->getStartDate(); // -05-06 00:00:00

echo $period->getEndDate(); // -05-25 00:00:00

echo $period->getDateInterval(); // 1 day

echo $exclude ? 'exclude' : 'include'; // exclude

var_dump($period->isStartExcluded()); // bool(true)

var_dump($period->isEndExcluded()); // bool(false)

echo $period->toString(); // Every 1 day from -05-06 to -05-25

echo $period; /*implicit toString*/ // Every 1 day from -05-06 to -05-25

附加的getter允许您以数组的形式访问结果:

$period = CarbonPeriod::create('-05-11', '-05-13');

echo $period->count(); // 3, equivalent to count($period)

echo implode(', ', $period->toArray()); // -05-11 00:00:00, -05-12 00:00:00, -05-13 00:00:00

echo $period->first(); // -05-11 00:00:00

echo $period->last(); // -05-13 00:00:00

注意,如果您打算使用上述函数,将toArray()调用的结果存储为变量并使用它是一个好主意,因为每个调用在内部执行一个完整的迭代。

想要更改参数,可以使用setter方法:

$period = CarbonPeriod::create('-05-01', '-05-14', CarbonPeriod::EXCLUDE_END_DATE);

$period->setStartDate('-05-11');

echo implode(', ', $period->toArray()); // -05-11 00:00:00, -05-12 00:00:00, -05-13 00:00:00

// Second argument can be optionally used to exclude the date from the results.

$period->setStartDate('-05-11', false);

$period->setEndDate('-05-14', true);

echo implode(', ', $period->toArray()); // -05-12 00:00:00, -05-13 00:00:00, -05-14 00:00:00

$period->setRecurrences(2);

echo implode(', ', $period->toArray()); // -05-12 00:00:00, -05-13 00:00:00

$period->setDateInterval('PT12H');

echo implode(', ', $period->toArray()); // -05-11 12:00:00, -05-12 00:00:00

您可以使用setOptions()更改选项以替换所有选项,但也可以分别更改:

$period = CarbonPeriod::create('-05-06', '-05-25');

var_dump($period->isStartExcluded()); // bool(false)

var_dump($period->isEndExcluded()); // bool(false)

$period->toggleOptions(CarbonPeriod::EXCLUDE_START_DATE, true); // true, false or nothing to invert the option

var_dump($period->isStartExcluded()); // bool(true)

var_dump($period->isEndExcluded()); // bool(false) (unchanged)

$period->excludeEndDate(); // specify false to include, true or omit to exclude

var_dump($period->isStartExcluded()); // bool(true) (unchanged)

var_dump($period->isEndExcluded()); // bool(true)

$period->excludeStartDate(false); // specify false to include, true or omit to exclude

var_dump($period->isStartExcluded()); // bool(false)

var_dump($period->isEndExcluded()); // bool(true)

如前所述,根据ISO 8601规范,递归是重复间隔的数倍。因此,本机DatePeriod将根据开始日期的排除而改变返回日期的数量。与此同时,CarbonPeriod在输入和允许自定义过滤器方面更加宽容,将递归作为返回日期的总体限制:

$period = CarbonPeriod::createFromIso('R4/-07-01T00:00:00Z/P7D');

$days = array();

foreach ($period as $date) {

$days[] = $date->format('d');

}

echo $period->getRecurrences(); // 4

echo implode(', ', $days); // 01, 08, 15, 22

$days = array();

$period->setRecurrences(3)->excludeStartDate();

foreach ($period as $date) {

$days[] = $date->format('d');

}

echo $period->getRecurrences(); // 3

echo implode(', ', $days); // 08, 15, 22

$days = array();

$period = CarbonPeriod::recurrences(3)->sinceNow();

foreach ($period as $date) {

$days[] = $date->format('Y-m-d');

}

echo implode(', ', $days); // -07-05, -07-06, -07-07

DatePeriod返回的日期可以很容易地过滤。例如,过滤器可以用于跳过某些日期或只在工作日或周末迭代。筛选函数应该返回true以接受日期,返回false以跳过日期,但继续搜索或CarbonPeriod::END_ITERATION以结束迭代。

$period = CarbonPeriod::between('2000-01-01', '2000-01-15');

$weekendFilter = function ($date) {

return $date->isWeekend();

};

$period->filter($weekendFilter);

$days = array();

foreach ($period as $date) {

$days[] = $date->format('m-d');

}

echo implode(', ', $days); // 01-01, 01-02, 01-08, 01-09, 01-15

您还可以跳过循环中的一个或多个值。

$period = CarbonPeriod::between('2000-01-01', '2000-01-10');

$days = array();

foreach ($period as $date) {

$day = $date->format('m-d');

$days[] = $day;

if ($day === '01-04') {

$period->skip(3);

}

}

echo implode(', ', $days); // 01-01, 01-02, 01-03, 01-04, 01-08, 01-09, 01-10

getFilters()允许您在一个时间段内检索所有存储的过滤器。但是要注意递归限制和结束日期将出现在返回的数组中,因为它们作为过滤器存储在内部。

$period = CarbonPeriod::end('2000-01-01')->recurrences(3);

var_export($period->getFilters());

/*

array (

0 =>

array (

0 => 'Carbon\\CarbonPeriod::filterEndDate',

1 => NULL,

),

1 =>

array (

0 => 'Carbon\\CarbonPeriod::filterRecurrences',

1 => NULL,

),

)

*/

过滤器存储在堆栈中,可以使用一组特殊的方法进行管理:

$period = CarbonPeriod::between('2000-01-01', '2000-01-15');

$weekendFilter = function ($date) {

return $date->isWeekend();

};

var_dump($period->hasFilter($weekendFilter)); // bool(false)

$period->addFilter($weekendFilter);

var_dump($period->hasFilter($weekendFilter)); // bool(true)

$period->removeFilter($weekendFilter);

var_dump($period->hasFilter($weekendFilter)); // bool(false)

// To avoid storing filters as variables you can name your filters:

$period->prependFilter(function ($date) {

return $date->isWeekend();

}, 'weekend');

var_dump($period->hasFilter('weekend')); // bool(true)

$period->removeFilter('weekend');

var_dump($period->hasFilter('weekend')); // bool(false)

添加过滤器的顺序会对性能和结果产生影响,因此您可以使用addFilter()在堆栈末尾添加过滤器;您可以使用prependFilter()在开始时添加一个。甚至可以使用setfilter()替换所有的过滤器。请注意,您必须保持堆栈的正确格式,并记住关于递归限制和结束日期的内部过滤器。或者,您可以使用resetFilters()方法,然后逐个添加新的过滤器。

例如,当您添加一个限制尝试日期数量的自定义过滤器时,如果您在工作日过滤器之前或之后添加它,那么结果将是不同的。

// Note that you can pass a name of any Carbon method starting with "is", including macros

$period = CarbonPeriod::between('-05-03', '-05-25')->filter('isWeekday');

$attempts = 0;

$attemptsFilter = function () use (&$attempts) {

return ++$attempts <= 5 ? true : CarbonPeriod::END_ITERATION;

};

$period->prependFilter($attemptsFilter, 'attempts');

$days = array();

foreach ($period as $date) {

$days[] = $date->format('m-d');

}

echo implode(', ', $days); // 05-03, 05-04, 05-07

$attempts = 0;

$period->removeFilter($attemptsFilter)->addFilter($attemptsFilter, 'attempts');

$days = array();

foreach ($period as $date) {

$days[] = $date->format('m-d');

}

echo implode(', ', $days); // 05-03, 05-04, 05-07, 05-08, 05-09

注意,内置的递归过滤器不是这样工作的。相反,它基于当前键,每个条目只增加一次,无论在找到有效日期之前需要检查多少个日期。如果您将它放在堆栈的开头或末尾,那么这个技巧将使它的工作方式相同。

为了简化CarbonPeriod的构建,添加了一些别名:

// "start", "since", "sinceNow":

CarbonPeriod::start('-03-10') == CarbonPeriod::create()->setStartDate('-03-10');

// Same with optional boolean argument $inclusive to change the option about include/exclude start date:

CarbonPeriod::start('-03-10', true) == CarbonPeriod::create()->setStartDate('-03-10', true);

// "end", "until", "untilNow":

CarbonPeriod::end('-03-20') == CarbonPeriod::create()->setEndDate('-03-20');

// Same with optional boolean argument $inclusive to change the option about include/exclude end date:

CarbonPeriod::end('-03-20', true) == CarbonPeriod::create()->setEndDate('-03-20', true);

// "dates", "between":

CarbonPeriod::dates(..., ...) == CarbonPeriod::create()->setDates(..., ...);

// "recurrences", "times":

CarbonPeriod::recurrences(5) == CarbonPeriod::create()->setRecurrences(5);

// "options":

CarbonPeriod::options(...) == CarbonPeriod::create()->setOptions(...);

// "toggle":

CarbonPeriod::toggle(..., true) == CarbonPeriod::create()->toggleOptions(..., true);

// "filter", "push":

CarbonPeriod::filter(...) == CarbonPeriod::create()->addFilter(...);

// "prepend":

CarbonPeriod::prepend(...) == CarbonPeriod::create()->prependFilter(...);

// "filters":

CarbonPeriod::filters(...) == CarbonPeriod::create()->setFilters(...);

// "interval", "each", "every", "step", "stepBy":

CarbonPeriod::interval(...) == CarbonPeriod::create()->setDateInterval(...);

// "invert":

CarbonPeriod::invert() == CarbonPeriod::create()->invertDateInterval();

// "year", "months", "month", "weeks", "week", "days", "dayz", "day",

// "hours", "hour", "minutes", "minute", "seconds", "second":

CarbonPeriod::hours(5) == CarbonPeriod::create()->setDateInterval(new CarbonInterval::hours(5));

可以很容易地将CarbonPeriod转换为人类可读的字符串和ISO 8601规范:

$period = CarbonPeriod::create('2000-01-01 12:00', '3 days 12 hours', '2000-01-15 12:00');

echo $period->toString(); // Every 3 days 12 hours from 2000-01-01 12:00:00 to 2000-01-15 12:00:00

echo $period->toIso8601String(); // 2000-01-01T12:00:00-05:00/P3DT12H/2000-01-15T12:00:00-05:00

英文原档:/docs/#api-interval

翻译有问题的地方还请斧正~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。