lib16/calendar

创建日历数据的PHP 7基本类。

v1.1 2020-05-26 08:23 UTC

This package is auto-updated.

Last update: 2024-09-14 20:35:39 UTC


README

Build Status

使用Composer安装

此软件包可在 Packagist 上找到,因此您可以使用 Composer 进行安装

composer require lib16/calendar

Calendar

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

use Lib16\Calendar\Calendar;
use Lib16\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);

生成的输出

{
    "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",


DateTime

DateTime 扩展了同名的标准 PHP 类

假设当前日期是

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

use Lib16\Calendar\DateTime;

const LN = "\n";

print      new DateTime();
print LN . new DateTime('2017-02-22');
print LN . new DateTime('first day of next month');

生成的输出

Thu, 23 Feb 2017 20:59:49 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Wed, 01 Mar 2017 20:59:49 +0100

create 方法

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

use Lib16\Calendar\DateTime;

const LN = "\n";

print      DateTime::create();
print LN . DateTime::create(new DateTime());
print LN . DateTime::create(new \DateTime());
print LN;
print LN . DateTime::create('2017-02-22');
print LN . DateTime::create('2017-02');
print LN . DateTime::create('22.02.2017');
print LN . DateTime::create('02/22/2017');
print LN . DateTime::create('last day of previous month');
print LN . DateTime::create('first monday of june 2017');
print LN . DateTime::create('now');
print LN;
print LN . DateTime::create(22, 2, 2017);
print LN . DateTime::create(22, 2);
print LN . DateTime::create(22);
print LN . DateTime::create(2017, 2, 22);
print LN . DateTime::create(2017, 2);
print LN . DateTime::create(2017);

生成的输出

Thu, 23 Feb 2017 00:00:00 +0100
Thu, 23 Feb 2017 00:00:00 +0100
Thu, 23 Feb 2017 00:00:00 +0100

Wed, 22 Feb 2017 00:00:00 +0100
Wed, 01 Feb 2017 00:00:00 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Tue, 31 Jan 2017 20:59:49 +0100
Mon, 05 Jun 2017 00:00:00 +0200
Thu, 23 Feb 2017 20:59:49 +0100

Wed, 22 Feb 2017 00:00:00 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Wed, 22 Feb 2017 00:00:00 +0100
Thu, 23 Feb 2017 00:00:00 +0100
Thu, 23 Feb 2017 00:00:00 +0100

easter 方法

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

use Lib16\Calendar\DateTime;

print DateTime::easter(2017);

生成的输出

Sun, 16 Apr 2017 00:00:00 +0200

修改日期

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

use Lib16\Calendar\DateTime;

const LN = "\n";

print LN . DateTime::create('2016-03-29')->addYears(2);
print LN . DateTime::create('2016-03-29')->addMonths(-2);
print LN . DateTime::create('2016-03-29')->addDays(3);
print LN . DateTime::create('2016-04-01')->forceWorkday();
print LN . DateTime::create('2016-04-02')->forceWorkday();
print LN . DateTime::create('2016-04-03')->forceWorkday();
print LN . DateTime::create('2016-04-04')->forceWorkday();

生成的输出


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

使用 copy 方法进行克隆

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

use Lib16\Calendar\DateTime;

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

print $pentecost;

生成的输出

Sun, 15 May 2016 00:00:00 +0200

formatLocalized 方法

此方法根据区域设置返回一个字符串表示形式。有关在格式字符串中可使用的指定符,请参阅 https://php.ac.cn/manual/en/function.strftime.php

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

use Lib16\Calendar\DateTime;

setlocale(LC_TIME, 'de');

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

生成的输出

Sonntag, 5. Juni 2016