Retrieve and easily manipulate Years, Months, Weeks, Days, Custom Ranges.
Excepting the range class, all the period types can be generated using the CalendR factory, wich is the simplest way.
<?php
$factory = new CalendR\Calendar;
$year = $factory->getYear($year);
$month = $factory->getMonth($year, $month);
$week = $factory->getWeek($year, $weeknum);
$day = $factory->getDay($year, $month, $day);
All periods implements PeriodInterface, and implement the following methods :
getBegin()
: Returns the begin \DateTime of the period. The begin is included in the periodgetEnd()
: Returns the end \DateTime of the period. The end is excluded from the periodgetNext()
: Returns the next period of the same type.getPrevious()
: Returns the previous period of the same type.getDatePeriod()
: Returns the period as a PHP \DatePeriod.contains()
: Returns if the given \DateTime is included in the periodcontainsPeriod()
: Same as contains() but for CalendR periodsequals()
: Returns if the period is equal to a PHP \DatePeriodincludes()
: Returns if a PHP \DatePeriod is included into the periodcontainsEvent()
: Returns if an Event is during the period. See Eventsformat()
: Format the period following the given pattern.Most of periods also implements :
* getDateInterval()
: a static method that returns the \DateInterval for the period type. Extremely useful to use with \DateTime::add() and \DateTime::sub()
* __toString()
: Returns your period as string, useful for debugging.
The Month period has an extra method, getExtendedMonth()
that return a Range
Period, begining at the Monday of the
first week of month, and ending at the Sunday of last week of month. This is useful for event retrieving, because the
“extended month” fit with usual calendar view.
You can iterate other Year, Month, and Week.
This allows you to create calendars in seconds :
<?php
// Use the factory to get your period
$factory = new CalendR\Calendar;
$month = $factory->getMonth(2012, 01);
?>
<table>
<?php // Iterate over your month and get weeks ?>
<?php foreach ($month as $week): ?>
<tr>
<?php // Iterate over your month and get days ?>
<?php foreach ($week as $day): ?>
<td><?php echo $day ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<?php $month = $factory->getMonth(2012, 01) ?>
<table>
<?php // Iterate over your month and get weeks ?>
<?php foreach ($month as $week): ?>
<tr>
<?php // Iterate over your month and get days ?>
<?php foreach ($week as $day): ?>
<?php //Check days that are out of your month ?>
<td<?php if (!$month->includes($day)) echo ' class="out-of-month"' ?>>
<?php echo $day ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<?php $week = $factory->getWeek(2012, 14) ?>
<table>
<thead>
<tr>
<?php foreach ($week as $day): ?>
<th><?php echo $day ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($week as $day): ?>
<td>
<?php // Retrieve your events, for exemple ?>
</td>
<?php endforeach ?>
</tr>
</tbody>
</table>