benhall14/php-calendar

一个用于生成日历的简单PHP类。

1.5.4 2024-09-23 09:41 UTC

This package is auto-updated.

Last update: 2024-09-23 09:42:45 UTC


README

一个使生成日历尽可能简单的PHP类。

您可以使用addEvent()或addEvents()方法在生成的日历上标记事件。

该类完全兼容PHP 5至PHP 8.1+

通过Composer安装

您现在可以通过composer安装此类。

$ composer require benhall14/php-calendar

记住在使用类之前添加composer自动加载器,并使用正确的命名空间。

require 'vendor/autoload.php';

use benhall14\phpCalendar\Calendar as Calendar;

使用方法

请确保您已经添加了所需的类。

样式

您可以通过以下三种方式之一应用样式

  1. 在初始化日历后使用$calendar->stylesheet();
    $calendar = new Calendar;
    $calendar->stylesheet();
  1. 使用css目录中的calendar.css(或calendar.min.css);
    <link rel="stylesheet" type="text/css" href="css/calendar.min.css">
  1. 创建自己的样式表并将其添加到HTML文档的head部分。

绘制“月份视图”日历

在其最简单形式中,使用以下内容创建当前月份的日历。这将使用所有默认值(英语,周从星期日开始,包括样式表,打印到页面)。

    (new Calendar)->display();

或者,您可以进行完全自定义

    # create the calendar object
    $calendar = new Calendar;
    
    # change the weekly start date to "Monday"
    $calendar->useMondayStartingDate();
    
    # or revert to the default "Sunday"
    $calendar->useSundayStartingDate();
    
    # (optional) - if you want to use full day names instead of initials (ie, Sunday instead of S), apply the following:
    $calendar->useFullDayNames();
    
    # to revert to initials, use:
    $calendar->useInitialDayNames();

    # add your own table class(es)
    $calendar->addTableClasses('class-1 class-2 class-3');
    # or using an array of classes.
    $calendar->addTableClasses(['class-1', 'class-2', 'class-3']);
    
    # (optional) - if you want to hide certain weekdays from the calendar, for example a calendar without weekends, you can use the following methods:
    $calendar->hideSaturdays() 		# This will hide Saturdays
    $calendar->hideSundays(); 		# This will hide Sundays
    $calendar->hideMondays(); 		# This will hide Mondays
    $calendar->hideTuesdays(); 		# This will hide Tuesdays
    $calendar->hideWednesdays();	# This will hide Wednesdays
    $calendar->hideThursdays();		# This will hide Thursdays
    $calendar->hideFridays();		# This will hide Fridays
    
    # (optional) - Translated Calendars - currently, there is only Spanish, but see "Translations" below for adding your own strings.
    $calendar->useSpanish(); 

    # if needed, add event
	$calendar->addEvent(
	    '2022-01-14',   # start date in either Y-m-d or Y-m-d H:i if you want to add a time.
	    '2022-01-14',   # end date in either Y-m-d or Y-m-d H:i if you want to add a time.
	    'My Birthday',  # event name text
	    true,           # should the date be masked - boolean default true
	    ['myclass', 'abc']   # (optional) additional classes in either string or array format to be included on the event days
	    ['event-class', 'abc']   # (optional) additional classes in either string or array format to be included on the event summary box
	);

    # or for multiple events

	$events = array();

	$events[] = array(
		'start' => '2022-01-14',
		'end' => '2022-01-14',
		'summary' => 'My Birthday',
		'mask' => true,
		'classes' => ['myclass', 'abc'],
        'event_box_classes' => ['event-box-1']
	);

	$events[] = array(
		'start' => '2022-12-25',
		'end' => '2022-12-25',
		'summary' => 'Christmas',
		'mask' => true
	);

	$calendar->addEvents($events);

    # finally, to draw a calendar    
    echo $calendar->draw(date('Y-m-d')); # draw this months calendar    

    # this can be repeated as many times as needed with different dates passed, such as:    
    echo $calendar->draw(date('Y-01-01')); # draw a calendar for January this year    
    echo $calendar->draw(date('Y-02-01')); # draw a calendar for February this year    
    echo $calendar->draw(date('Y-03-01')); # draw a calendar for March this year    
    echo $calendar->draw(date('Y-04-01')); # draw a calendar for April this year    
    echo $calendar->draw(date('Y-05-01')); # draw a calendar for May this year    
    echo $calendar->draw(date('Y-06-01')); # draw a calendar for June this year    
    
    # to use the pre-made color schemes, call the ->stylesheet() method and then pass the color choice to the draw method, such as:    
    echo $calendar->draw(date('Y-m-d'));            # print a (default) turquoise calendar    
    echo $calendar->draw(date('Y-m-d'), 'purple');  # print a purple calendar    
    echo $calendar->draw(date('Y-m-d'), 'pink');    # print a pink calendar    
    echo $calendar->draw(date('Y-m-d'), 'orange');  # print a orange calendar    
    echo $calendar->draw(date('Y-m-d'), 'yellow');  # print a yellow calendar    
    echo $calendar->draw(date('Y-m-d'), 'green');   # print a green calendar    
    echo $calendar->draw(date('Y-m-d'), 'grey');    # print a grey calendar    
    echo $calendar->draw(date('Y-m-d'), 'blue');    # print a blue calendar  
    
    # you can also call ->display(), which handles the echo'ing and adding the stylesheet.
    echo $calendar->display(date('Y-m-d')); # draw this months calendar    

绘制“周视图”日历

您现在可以渲染为“周视图”。为此,只需使用->useWeekView()方法即可。记住,当向周视图日历添加事件时,需要包括时间(见上方事件)。

    $events = array();

    $events[] = array(
        'start' => '2022-01-14 14:40',
        'end' => '2022-01-14 15:10',
        'summary' => 'My Birthday',
        'mask' => true,
        'classes' => ['myclass', 'abc']
    );

    $events[] = array(
        'start' => '2022-11-04 14:00',
        'end' => '2022-11-04 18:30',
        'summary' => 'Event 1',
        'mask' => true
    );
    $events[] = array(
        'start' => '2022-11-04 14:00',
        'end' => '2022-11-04 18:30',
        'summary' => 'Event 2',
        'mask' => true
    );

    $calendar = new Calendar;

    $calendar->addEvents($events)->setTimeFormat('00:00', '00:00', 10)->useWeekView()->display(date('Y-m-d'), 'green');

您可以使用->setTimeFormat方法更改一天的开始/结束时间以及时间间隔

    $calendar->setTimeFormat('00:00', '00:00', 10)->useWeekView()->display(date('Y-m-d'), 'green');
    # This will print a week view calendar with 10 minute slots from midnight to midnight - ie. 00:00, 00:10, 00:20 and so on.

星期一开始日期

现在,您可以将每周的开始日期从<强>星期日更改为<强>星期一。要激活此功能,请在“绘制”之前简单使用<强>useMondayStartingDate()方法。

    $calendar = new Calendar;
    $calendar->useMondayStartingDate();
    $calendar->display(date('Y-m-d'), 'green');

翻译后的日历

我们现在提供英语和西班牙语翻译,更多语言即将推出。或者,您可以使用以下方式添加自己的自定义字符串翻译,包括星期和月份

    	
    # This will set up the days - simply copy/paste the code below and replace the Spanish initials and full-day names with your own. (NB - Leave the keys in English)
    $calendar->setDays([
        'sunday' => [
        	'initials' => 'D',
        	'full' => 'Domingo'
        ],
        'monday' => [
        	'initials' => 'L',
        	'full' => 'Lunes',
        ],
        'tuesday' => [
        	'initials' => 'M',
        	'full' => 'Martes',
        ],
        'wednesday' => [
        	'initials' => 'X',
        	'full' => 'Miércoles',
        ],
        'thursday' => [
        	'initials' => 'J',
        	'full' => 'Jueves',
        ],
        'friday' => [
        	'initials' => 'V',
        	'full' => 'Viernes',
        ],
        'saturday' => [
        	'initials' => 'S',
        	'full' => 'Sábado',
        ],
    ]);
    
    # To add custom month names, simply copy/paste the code below and replace the Spanish month names with your own strings. (NB - Leave the keys in English)
    $calendar->setMonths([
        'january' => 'Enero',  
        'february' => 'Febrero',  
        'march' => 'Marzo',  
        'april' => 'Abril',  
        'may' => 'Mayo',  
        'june' => 'Junio',  
        'july' => 'Julio',  
        'august' => 'Agosto',  
        'september' => 'Septiembre',  
        'october' => 'Octubre',  
        'november' => 'Noviembre',  
        'december' => 'Diciembre'
    ]);

如果您想帮助翻译,请以<强>useSpanish()方法中的代码为指南,并提交pull-request。

要求

已全面测试,适用于PHP 5.3、5.5、5.6、7.0、7.1、7.2、7.3和8.1

PHP DateTime

许可

版权(c)2016-2022 Benjamin Hall,ben@conobe.co.uk https://conobe.co.uk

在MIT许可证下发布

捐赠?

如果您发现这个项目在任何方面都有帮助或有用,请考虑为我买杯咖啡 - 真心感谢 :)

Donate