PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (9 months ago) RSS 2.0 feedStarStarStar 50%Total: 521 This week: 1All time: 5,704 This week: 34Up
Version License PHP version Categories
ssc 1.0.17MIT/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
  Performance   Level  
Name: Marcos Sigueros <contact>
Classes: 1 package by
Country: Spain Spain
Age: ???
All time rank: 322885 in Spain Spain
Week rank: 44 Up2 in Spain Spain Up

Example

<?php
/**
 * To execute this example you need to install the library with composer in order
 * to use the autoload in this include file.
 * If you dont wanna use composer just include each file from the src directory.
 */
 
//include 'calendar/src/SSC/Calendar.php';
//include 'calendar/src/SSC/CalendarConfig.php';
//include 'calendar/src/SSC/formatters/FormatterInterface.php';
//include 'calendar/src/SSC/formatters/ArrayFormatter.php';
//include 'calendar/src/SSC/formatters/ObjectFormatter.php';
//include 'calendar/src/SSC/formatters/JsonFormatter.php';

include '../vendor/autoload.php';

$cal = new \SSC\Calendar();

$cal->day_callback = function($date){
   
$day = new stdClass();
   
$day->has_passed = $date->getTimestamp()<time();
    return
$day;
};

$structure = $cal->getCalendarStructure();

?>
<h1>Spanish calendar</h1>

<?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; ?>


Details

Calendar Build Status

Latest Stable Version Total Downloads Latest Unstable Version License

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.

Take a look to this example render:

Calendar example

Installation

Requirements

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

With Composer

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

{
    "require": {
        "alrik11es/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 %}

Vue.js

With the boom of this kind of libraries just like angular or react. I give you an example of how a render should look like. Obviously you will need to make the API part. But at least this is a good starting point.

<template>

    <div>
        <div v-for="year in calendar">
            <!--<div>{{year.value}}</div>-->
            <div v-for="quarter in year.elements">
                <div v-for="month in quarter.elements" class="month">
                    {{ year.value }} - {{ 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>
                        <tr v-for="week in month.elements">
                            <td v-for="weekday in [1,2,3,4,5,6,0]">
                                <div v-for="day in week.elements">
                                    <span v-if="day.weekday == weekday">
                                        {{ day.value }}
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                calendar: {},
            }
        },
        methods: {
        },
        mounted() {
            axios.get('/api/calendar').then(response => {
                this.calendar = response.data;
                console.log(this.calendar);
            }).catch(e => {
            });
        }
    }
</script>


<style scoped lang="sass">
    .month{
        float: left;
        margin: 10px;
    }
</style>

  Files folder image Files (15)  
File Role Description
Files folder imageexamples (1 file)
Files folder imageimg (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 (15)  /  examples  
File Role Description
  Accessible without login Plain text file example1.php Example Example script

  Files folder image Files (15)  /  img  
File Role Description
  Accessible without login Image file calendar.png Icon Icon image

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

  Files folder image Files (15)  /  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 (15)  /  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 (15)  /  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 (15)  /  tests  /  unit  
File Role Description
  Accessible without login Plain text file SSCTest.php Test Unit test script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:521
This week:1
All time:5,704
This week:34Up
User Ratings User Comments (1)
 All time
Utility:66%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:-
Examples:50%StarStarStar
Tests:50%StarStarStar
Videos:-
Overall:50%StarStarStar
Rank:2643