Using Calends in PHP
Calends exposes a very small handful of things for use outside the library
itself. One is the Calends class, documented here, which should be
the only interface users of the library ever need to touch.
Another thing Calends exposes is the TAITime class, used to store
and manipulate the instants of time which make up a Calends
instance. The rest are interfaces for extending the library’s functionality.
These are covered in the Custom Calendars in PHP
section.
Примечание
Calends objects are immutable - all methods return a new
Calends object where they might otherwise alter the current one.
This has the side effect of using more memory to perform manipulations than
updating values on an existing object would. It makes many operations safer,
though, than mutable objects would allow.
- class Calends\Calends
The main entry point to Calends and its functionality.
Calendsobjects can only be created with theCalends\Calends::createfunction, and not directly.Calendsdoes implement theSerializableinterface, though, so it’s safe toserialize/unserializeinstances if you want.
Create
- static Calends\Calends::create($value, $calendar, $format)
- Параметры
$value (
mixed) – The value to parse the date(s)/time(s) from.$calendar (
string) – The calendar system to parse the date(s)/time(s) with.$format (
string) – The format the date(s)/time(s) is/are expected to use.
- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occurs
Creates a new
Calendsobject, using$calendarto select a calendar system, and$formatto parse the contents of$valueinto theCalendsobject’s internal instants. The type of$valuecan vary based on the calendar system itself, but generally speaking can always be a string.In any case, the value can always be an associative array, where the keys are any two of
start,end, andduration. If all three are provided,durationis ignored in favor of calculating it directly.The calendar system then converts
$valueto aTAITimeinstant, which theCalendsobject sets to the appropriate internal value.
Read
- Calends\Calends::date($calendar, $format)
- Параметры
$calendar (
string) – The calendar system to format the date/time with.$format (
string) – The format the date/time is expected to be in.- Результат
The start date of the
Calendsobject- Тип результата
string- Бросает исключение
CalendsException– when an error occursRetrieves the start date of the
Calendsobject as a string. The value is generated by the calendar system given in$calendar, according to the format string in$format.
- Calends\Calends::endDate($calendar, $format)
- Параметры
$calendar (
string) – The calendar system to format the date/time with.$format (
string) – The format the date/time is expected to be in.- Результат
The end date of the
Calendsobject- Тип результата
string- Бросает исключение
CalendsException– when an error occursRetrieves the end date of the
Calendsobject as a string. The value is generated by the calendar system given in$calendar, according to the format string in$format.
Update
- Calends\Calends::withDate($value, $calendar, $format)
- Параметры
$value (
mixed) – The value to parse the date/time from.$calendar (
string) – The calendar system to parse the date/time with.$format (
string) – The format the date/time is expected to use.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject with a start date based on the currentCalendsobject’s value. The inputs are the same as forCalends\Calends::create, above, except the string → value map option isn’t available, since you’re already specifically setting the start value explicitly.
- Calends\Calends::withEndDate($value, $calendar, $format)
- Параметры
$value (
mixed) – The value to parse the date/time from.$calendar (
string) – The calendar system to parse the date/time with.$format (
string) – The format the date/time is expected to use.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject with an end date based on the currentCalendsobject’s value. The inputs are the same as forCalends\Calends::create, above, except the string → value map option isn’t available, since you’re already specifically setting the end value explicitly.
- Calends\Calends::withDuration($duration, $calendar)
- Параметры
$duration (
string) – The value to parse the new duration from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject with a duration set by adjusting the currentCalendsobject’s end point, and using its start point as an anchor. The$durationvalue is interpreted by the calendar system given in$calendar, so is subject to any of its rules.
- Calends\Calends::withDurationFromEnd($duration, $calendar)
- Параметры
$duration (
string) – The value to parse the new duration from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject with a duration set by adjusting the currentCalendsobject’s start point, and using its end point as an anchor. The$durationvalue is interpreted by the calendar system given in$calendar, so is subject to any of its rules.
Manipulate
- Calends\Calends::add($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursIncreases the end date of the
Calendsobject’s current value by$offset, as interpreted by the calendar system given in$calendar, and returns a newCalendsobject with the result.
- Calends\Calends::addFromEnd($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursIncreases the start date of the
Calendsobject’s current value by$offset, as interpreted by the calendar system given in$calendar, and returns a newCalendsobject with the result.
- Calends\Calends::subtract($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursWorks the same as
add, except it decreases the start date, rather than increasing it.
- Calends\Calends::subtractFromEnd($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursWorks the same as
addFromEnd, except it decreases the end date, rather than increasing it.
- Calends\Calends::next($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject of$offsetduration (as interpreted by the calendar system given in$calendar), which abuts the currentCalendsobject’s value. If$offsetis empty,$calendaris ignored, and the current object’s duration is used instead.
- Calends\Calends::previous($offset, $calendar)
- Параметры
$offset (
string) – The value to parse the offset from.$calendar (
string) – The calendar system to parse the date/time with.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject of$offsetduration (as interpreted by the calendar system given in$calendar), which abuts the currentCalendsobject’s value. If$offsetis empty,$calendaris ignored, and the current object’s duration is used instead.
Combine
- Calends\Calends::merge($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to merge.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject spanning from the earliest start date to the latest end date between the currentCalendsobject and$c2.
- Calends\Calends::intersect($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to intersect.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject spanning the overlap between the currentCalendsobject and$c2. If the current object and$c2don’t overlap, throws an error.
- Calends\Calends::gap($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to gap.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occursReturns a
Calendsobject spanning the gap between the currentCalendsobject and$c2. If the current object and$c2overlap (and there is, therefore, no gap to return), throws an error.
Compare
- Calends\Calends::difference($c2, $mode)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.$mode (
string) – The comparison mode.- Результат
The difference, as a decimal string
- Тип результата
stringReturns the difference of the current
Calendsobject minus$c2, using$modeto select which values to use in the calculation. Valid$modes include:
start- use the start date of both the current object and$c2
duration- use the duration of both the current object and$c2
end- use the end date of both the current object and$c2
start-end- use the start of the current object, and the end of$c2
end-start- use the end of the current object, and the start of$c2
- Calends\Calends::compare($c2, $mode)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.$mode (
string) – The comparison mode.- Результат
A standard comparison result
- Тип результата
intReturns
-1if the currentCalendsobject is less than$c2,0if they are equal, and1if the current object is greater than$c2, using$modeto select which values to use in the comparison. Valid$modes are the same as forCalends\Calends::difference, above.
- Calends\Calends::contains($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the current
Calendsobject contains all of$c2.
- Calends\Calends::overlaps($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the current
Calendsobject overlaps with$c2.
- Calends\Calends::abuts($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the current
Calendsobject abuts$c2(that is, whether one begins at the same instant the other ends).
- Calends\Calends::isSame($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the current
Calendsobject covers the same span of time as$c2.
- Calends\Calends::isShorter($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the duration of the current
Calendsobject and$c2.
- Calends\Calends::isSameDuration($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the duration of the current
Calendsobject and$c2.
- Calends\Calends::isLonger($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the duration of the current
Calendsobject and$c2.
- Calends\Calends::isBefore($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the entirety of the current
Calendsobject with the start date of$c2.
- Calends\Calends::startsBefore($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the start date of the current
Calendsobject with the start date of$c2.
- Calends\Calends::endsBefore($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the end date of the current
Calendsobject with the start date of$c2.
- Calends\Calends::isDuring($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the entirety of the current
Calendsobject lies between the start and end dates of$c2.
- Calends\Calends::startsDuring($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the start date of the current
Calendsobject lies between the start and end dates of$c2.
- Calends\Calends::endsDuring($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolChecks whether the end date of the current
Calendsobject lies between the start and end dates of$c2.
- Calends\Calends::isAfter($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the entirety of the current
Calendsobject with the end date of$c2.
- Calends\Calends::startsAfter($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the start date of the current
Calendsobject with the end date of$c2.
- Calends\Calends::endsAfter($c2)
- Параметры
$c2 (
Calends\Calends) – TheCalendsobject to compare.- Результат
The result of the comparison
- Тип результата
boolCompares the end date of the current
Calendsobject with the end date of$c2.
Export
It’s possible to export Calends values in a couple of ways. It
implements Serializable and JsonSerializable, as well
as the __toString method, so the regular mechanisms for each of
those are readily available and usable. In addition, it also offers support for
JSON-decoding values directly:
- static Calends\Calends::jsonUnserialize($encoded)
- Параметры
$encoded (
string) – The JSON-encoded value to import.- Результат
A new
Calendsobject- Тип результата
- Бросает исключение
CalendsException– when an error occurs
Error Handling
- class Calends\CalendsException
A very simple exception class, directly extending
Exception. It is thrown by the library for all encountered errors.