The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Period API

Instantiation

To instantiate a Period object you can rely on its constructor or on several named constructors describe below.

The default constructor

Description

<?php

public Period::__construct(mixed $startDate, mixed $endDate)

Parameters

Both $startDate and $endDate parameters represent the period datepoints.

$endDate must be greater or equal to $startDate or the instantiation will throw a LogicException.

Examples

<?php

use League\Period\Period;

$period = new Period('2012-04-01 08:30:25', new DateTime('2013-09-04 12:35:21'));

Named constructors

Apart from its constructor, to ease the class instantiation you can rely on many built in named constructors to return a new Period object.

Create a new instance representing a given day

Since version 3.1

Description

<?php

public static Period::createFromDay(mixed $startDate): Period

Parameter

The $startDate represents the starting included datepoint; The date is truncated so that the duration starts at midnight according to the date timezone.

Example

<?php

use League\Period\Period;

$period = Period::createFromDay('2012-04-01 08:30:25');
$alt    = Period::createFromDay('2012-04-01');
$alt->sameValueAs($period); //return true;

Because of a bug in HHVM, when using createFromDay, the timezone information is lost.

Create a new instance representing a given week

Description

<?php

public static Period::createFromWeek(int $year, int $week): Period

Parameters

Example

<?php

use League\Period\Period;

$period = Period::createFromWeek(2013, 23);
//this period represents the 23rd week of 2013

The week index follows the ISO week date system. This means that the first week may be included in the previous year, conversely the last week may be included in the next year.

Create a new instance representing a given month

Description

<?php

public static Period::createFromMonth(int $year, int $month): Period

Parameters

Example

<?php

use League\Period\Period;

$period = Period::createFromMonth(2013, 7);
//this period represents the month of July 2013

Create a new instance representing a given quarter

Description

<?php

public static Period::createFromQuarter(int $year, int $quarter): Period

Parameters

Example

<?php

use League\Period\Period;

$period = Period::createFromQuarter(2013, 2);
//this period represents the second quarter of 2013

Create a new instance representing a given semester

Description

<?php

public static Period::createFromSemester(int $year, int $semester): Period

Parameters

Example

<?php

use League\Period\Period;

$period = Period::createFromSemester(2013, 2);
//this period represents the second semester of 2013

Create a new instance representing a given year

Description

<?php

public static Period::createFromYear(int $year): Period

Parameter

The $year parameter must be a valid year;

Example

<?php

use League\Period\Period;

$period = Period::createFromYear(2013);
//this period represents a time range for 2013

Create a new instance from a date and a duration

Description

<?php

public static Period::createFromDuration(mixed $startDate, mixed $duration): Period
public static Period::createFromDurationBeforeEnd(mixed $endDate, mixed $duration): Period

Both created Period objects will have a duration equals to $duration.

Parameters

Example

<?php

use League\Period\Period;

$period = Period::createFromDuration('2012-04-01 08:30:25', '1 DAY');
$alt    = Period::createFromDurationBeforeEnd('2012-04-02 08:30:25', new DateInterval('P1D'));
$alt->sameValueAs($period); //returns true

Using __set_state

Since version 3.2

PHP’s magic method __set_state is now supported.

<?php

use League\Period\Period;

$period = Period::createFromDuration('2012-04-01 08:30:25', '1 DAY');
$alt = eval('return '.var_export($period, true).';');

$alt->sameValueAs($period); //returns true

Because of a bug in HHVM, This method is not supported in HHVM.