PHP Classes
elePHPant
Icontem

Stupidly Simple PHP Calendar: Render calendars as arrays of time elements

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files Example   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2015-08-15 (14 days ago) RSS 2.0 feedNot enough user ratingsTotal: 402 This week: 2All time: 6,043 This week: 570Up
Version License PHP version Categories  
ssc 1.0.4MIT/X Consortium ...5.4PHP 5, Time and Date
Description Author  

This class can render calendars as arrays of time elements.

It takes a given start date and period length and generates arrays of time elements that describe what should be rendered on calendars for that period like years, quarters, months, weeks and days.

The generated time elements array can be filtered by one of several possible formatter classes that can transform the array into a more useful format.

Currently it provides formatter classes that can transform the array in a JSON format string or an object.

Picture of Marcos Sigueros
Name: Marcos Sigueros <contact>
Classes: 1 package by
Country: Spain Spain
Age: ???
All time rank: 323185 in Spain Spain
Week rank: 542 Up15 in Spain Spain Up

Details provided by the author  

UCNU Calendar Build Status

Latest Stable Version Total Downloads Latest Unstable Version License

UCNU Calendar is a PHP calendar structure generator in array, object or JSON format.

Motivation

Do you remember those long... long... hours trying to create a rendered calendar? Think about this, the main problem is that you have to generate the structure to do the rendering in your template engine, no matter what it is. So the thing here is I don't wanna give you a fully rendered calendar just the structure you need to do the render. How do you do the render... it's all on you.

Installation

Requirements

  • Any flavour of PHP 5.4+ should do
  • [optional] PHPUnit to execute the test suite

With Composer

The easiest way to install SSC is via composer. Create the following composer.json file and run the php composer.phar install command to install it.

{
    "require": {
        "ucnu/calendar": "0.1.*"
    }
}

No composer

Git clone this repo where you want and include/require all the src folder into your project.

How it works

Lets get into business. Set up a calendar from today to 6 months into the future:

$cal = new \SSC\Calendar();
$structure = $cal->getCalendar();

print_r($structure);

The output then is like the next one:

Array
(
[2014] => Array
    (
    [type] => year
    [value] => 2014
    [data] =>
    [elements] => Array
        (
        [3] => Array
            (
            [type] => quarter
            [value] => 3
            [data] =>
            [elements] => Array
                (
                [8] => Array
                    (
                    [type] => month
                    [value] => 8
                    [data] =>
                    [elements] => Array
                        (
                        [31] => Array
                            (
                            [type] => week
                            [value] => 31
                            [data] =>
                            [elements] => Array
                                (
                                [1] => Array
                                    (
                                    [type] => day
                                    [value] => 1
                                    [data] => 
                                    [weekday] => 5
                                    )

Then you just have to take it and render in your Twig or whatever... (Next one is the spanish way, but you can change to whatever you want)

<?php foreach($structure as $year): ?>
    <?php foreach($year['elements'] as $quarter): ?>
        <?php foreach($quarter['elements'] as $month): ?>
            <div>
                <?php echo $year['value']; ?> - <?php echo $month['value']; ?>
                <table>
                    <tr>
                        <th>Mon</th>
                        <th>Tue</th>
                        <th>Wed</th>
                        <th>Thu</th>
                        <th>Fra</th>
                        <th>Sun</th>
                        <th>Sat</th>
                    </tr>
                    <?php foreach($month['elements'] as $week): ?>
                        <tr>
                            <?php foreach(array(1,2,3,4,5,6,0) as $weekday): ?>
                                <td>
                                <?php foreach($week['elements'] as $day): ?>
                                    <?php if($day['weekday'] == $weekday): ?>
                                        <?php echo $day['value'];?>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                </td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        <?php endforeach; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

Bam! Calendar!

Oh BTW I can give you a Twig example Just for the record :P

$cal = new \SSC\Calendar();
$cal->getConfig()->setInterval(new \DateInterval('P12M'));
$cal->day_callback = function($date){
    $day = new \stdClass();
    $day->has_passed = $date->getTimestamp()<time();
    return $day;
};

// Spanish months (There is tons of ways of doing this but...)
$context['months_es'] = array(
    1=>'Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'
);
// The week days order. In spanish calendar this is different than in english.
$context['week_days'] = array(1,2,3,4,5,6,0);
// The calendar structure...
$context['cal'] = $cal->getCalendarStructure();

Yeah, the Twig file...

{% for year in cal %}
    {# You could add here the years #}
    {% for quarter in year.elements %}
        {% for month in quarter.elements %}
        <table class="month">
            <tbody>
            <tr>
                <th colspan="9" class="month-title">{{ year.value }} - {{ months_es[month.value] }}</th>
            </tr>
            <tr>
                <th>L</th>
                <th>M</th>
                <th>X</th>
                <th>J</th>
                <th>V</th>
                <th class="thweekend">S</th>
                <th class="thweekend">D</th>
            </tr>
            </tbody>

            {% for week in month.elements %}
                <tr>
                    {# You could add here the week number #}
                    {% for week_day in week_days%}
                        <td class="day-container">
                            {% for day in week.elements %}
                                {% if day.weekday == week_day %}
                                    <div class="day {% if day.data.has_passed %}passed_day{% endif %} {% if week_day == 6 or week_day == 0 %}weekend{% endif %}">
                                        {{ day.value }}
                                    </div>
                                {% endif %}
                            {% endfor %}
                        </td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </table>
        {% endfor %}
    {% endfor %}
{% endfor %}
  Files folder image Files  
File Role Description
Files folder imageexamples (1 file)
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file example1.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageSSC (2 files, 1 directory)

  Files folder image Files  /  src  /  SSC  
File Role Description
Files folder imageformatters (4 files)
  Plain text file Calendar.php Class Class source
  Plain text file CalendarConfig.php Class Class source

  Files folder image Files  /  src  /  SSC  /  formatters  
File Role Description
  Plain text file ArrayFormatter.php Class Class source
  Plain text file FormatterInterface.php Class Class source
  Plain text file JsonFormatter.php Class Class source
  Plain text file ObjectFormatter.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (1 file)
  Accessible without login Plain text file bootstrap.php Aux. Unit test script

  Files folder image Files  /  tests  /  unit  
File Role Description
  Accessible without login Plain text file SSCTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%Total:402All time:6,043
 This week:2This week:570Up
 User Comments (1)