2017 © Pedro Peláez
 

library calendar

Basic calendar classes.

image

clacy-builders/calendar

Basic calendar classes.

  • Friday, May 13, 2016
  • by clacy-builders
  • Repository
  • 1 Watchers
  • 1 Stars
  • 14 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

clacy-builders/calendar

Installation

This library requires PHP 5.4 or newer., (*1)

Add the following to your project's composer.json file:, (*2)

{
    "minimum-stability": "dev",
    "require": {
        "clacy-builders/calendar": "dev-master"
    }
}

Run composer install or composer update., (*3)

Basic Usage

The Calendar class

<?php
require_once 'vendor/autoload.php';

use \ClacyBuilders\Calendar\Calendar;
use \ClacyBuilders\Calendar\DateTime;

setlocale(LC_TIME, 'de');
$easter = DateTime::easter(2016);
$calendar = Calendar::month(5, 2016)
        ->setMonthFormat('%b %Y')
        ->setFirstWeekday('DE')
        ->addEntry('2016-05-01', 'Tag der Arbeit')
        ->addEntry($easter->copy()->addDays(39), 'Christi Himmelfahrt')
        ->addEntry($easter->copy()->addDays(50), 'Pfingstmontag')
        ->addEntry($easter->copy()->addDays(60), 'Fronleichnam');
print json_encode($calendar->buildArray(), JSON_PRETTY_PRINT);

The generated JSON text:, (*4)

{
    "weekdays": {
        "mon": "Mo",
        "tue": "Di",
        "wed": "Mi",
        "thu": "Do",
        "fri": "Fr",
        "sat": "Sa",
        "sun": "So"
    },
    "years": [
        {
            "time": "2016",
            "label": "2016",
            "months": [
                {
                    "time": "2016-05",
                    "label": "Mai 2016",
                    "month": "05",
                    "weeks": [
                        {
                            "time": "2016-W17",
                            "label": "17",
                            "leading": 6,
                            "days": [
                                {
                                    "time": "2016-05-01",
                                    "label": "1",
                                    "weekday": "sun",
                                    "entries": [
                                        {
                                            "class": "holiday",
                                            "title": "Tag der Arbeit"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "time": "2016-W18",
                            "label": "18",
                            "days": [
                                {
                                    "time": "2016-05-02",
                                    "label": "2",
                                    "weekday": "mon"
                                },
                                {
                                    "time": "2016-05-03",
                                    "label": "3",
                                    "weekday": "tue"
                                },
                                {
                                    "time": "2016-05-04",
                                    "label": "4",
                                    "weekday": "wed"
                                },
                                {
                                    "time": "2016-05-05",
                                    "label": "5",
                                    "weekday": "thu",
                                    "entries": [
                                        {
                                            "class": "holiday",
                                            "title": "Christi Himmelfahrt"
                                        }
                                    ]
                                },
                                {
                                    "time": "2016-05-06",
                                    "label": "6",
                                    "weekday": "fri"
                                },
                                {
                                    "time": "2016-05-07",
                                    "label": "7",
                                    "weekday": "sat"
                                },
                                {
                                    "time": "2016-05-08",

…

See also: https://github.com/clacy-builders/html5-express-php/wiki/Calendar, (*5)

The DateTime class

require_once 'vendor/autoload.php';
use \ClacyBuilders\Calendar\DateTime;

Constructor

DateTime extends the standard PHP class DateTime., (*6)

Assuming that current date is Mon, 04 Apr 2016 16:22:02 +0200:, (*7)

$days[] = new DateTime('2016-03-29');
$days[] = new DateTime();
$days[] = new DateTime('first day of next month');

The result:, (*8)

Tue, 29 Mar 2016 00:00:00 +0200
Mon, 04 Apr 2016 16:22:02 +0200
Sun, 01 May 2016 16:22:02 +0200

Factory Methods

Assuming that current date is Mon, 04 Apr 2016 16:22:02 +0200:, (*9)

$days[] = DateTime::create(29, 3, 2016);
$days[] = DateTime::create(29, 3);
$days[] = DateTime::create(29);
$days[] = DateTime::create(2016, 3, 29);
$days[] = DateTime::create(2016, 3);
$days[] = DateTime::create(2016);
$days[] = DateTime::create();
$days[] = DateTime::create('2016-03-29');
$days[] = DateTime::create('2016-03');
$days[] = DateTime::create('29.03.2016');
$days[] = DateTime::create('03/29/2016');
$days[] = DateTime::create('last day of previous month');
$days[] = DateTime::easter(2016);

The result:, (*10)

Tue, 29 Mar 2016 00:00:00 +0200
Tue, 29 Mar 2016 00:00:00 +0200
Fri, 29 Apr 2016 00:00:00 +0200
Tue, 29 Mar 2016 00:00:00 +0200
Fri, 04 Mar 2016 00:00:00 +0100
Mon, 04 Apr 2016 00:00:00 +0200
Mon, 04 Apr 2016 00:00:00 +0200
Tue, 29 Mar 2016 00:00:00 +0200
Tue, 01 Mar 2016 00:00:00 +0100
Tue, 29 Mar 2016 00:00:00 +0200
Tue, 29 Mar 2016 00:00:00 +0200
Thu, 31 Mar 2016 16:22:02 +0200
Sun, 27 Mar 2016 00:00:00 +0100

Modify Dates

$days[] = DateTime::create('2016-03-29')->addYears(2);
$days[] = DateTime::create('2016-03-29')->addMonths(-2);
$days[] = DateTime::create('2016-03-29')->addDays(3);
$days[] = DateTime::create('2016-04-01')->forceWorkday();
$days[] = DateTime::create('2016-04-02')->forceWorkday();
$days[] = DateTime::create('2016-04-03')->forceWorkday();
$days[] = DateTime::create('2016-04-04')->forceWorkday();

The result:, (*11)

Thu, 29 Mar 2018 00:00:00 +0200
Fri, 29 Jan 2016 00:00:00 +0100
Fri, 01 Apr 2016 00:00:00 +0200
Fri, 01 Apr 2016 00:00:00 +0200
Fri, 01 Apr 2016 00:00:00 +0200
Mon, 04 Apr 2016 00:00:00 +0200
Mon, 04 Apr 2016 00:00:00 +0200

Clone with the copy method

$easter = DateTime::easter(2016);
$pentecost = $easter->copy()->addDays(49);

The result:, (*12)

Sun, 27 Mar 2016 00:00:00 +0100
Sun, 15 May 2016 00:00:00 +0200

The formatLocalized method

This method returns a string representation according to locale settings. http://php.net/manual/en/function.strftime.php lists the specifiers you can use in the format string., (*13)

setlocale(LC_TIME, 'de');
$date = DateTime::create('2016-06-05');
print $date->formatLocalized('%A, %#d. %B %Y');

The result:, (*14)

Sonntag, 5. Juni 2016

The Versions

13/05 2016

dev-master

9999999-dev

Basic calendar classes.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

08/04 2016

0.1

0.1.0.0

Basic calendar classes.

  Sources   Download

MIT

The Requires

  • php >=5.4.0