PHP输出日历

<style type="text/css">
    .prev {
        text-align: left;
    }

    .next {
        text-align: right;
    }

    .day,
    .month,
    .weekday {
        text-align: center;
    }

    .today {
        background: yellow;
        text-align: center;
    }

    .blank {}
</style>

<?php

class LittleCalendar
{
    protected $monthToUse;

    protected $prepared = false;

    protected $days = array();

    public function __construct($month, $year)
    {
        $this->monthToUse = DateTime::createFromFormat('Y-m|', sprintf('%04d-%02d', $year, $month));

        $this->prepare();

    }

    protected function prepare()
    {
        foreach (array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa') as $dow) {
            $endOfRow = ($dow == 'Sa');
            $this->days[] = array(
                'type' => 'dow',
                'label' => $dow,
                'endOfRow' => $endOfRow
            );
        }

        for ($i = 0, $j = $this->monthToUse->format('w'); $i < $j; $i++) {
            $this->days[] = array('type' => 'blank');
        }

        $today = date('Y-m-d');
        $days = new DatePeriod(
            $this->monthToUse,
            new DateInterval('P1D'),
            $this->monthToUse->format('t') - 1
        );

        foreach ($days as $day) {
            $isToday = ($day->format('Y-m-d') == $today);
            $endOfRow = ($day->format('w') == 6);

            $this->days[] = array(
                'type' => 'day',
                'label' => $day->format('j'),
                'today' => $isToday,
                'endOfRow' => $endOfRow
            );
        }

        if (!$endOfRow) {
            for ($i = 0, $j = 6 - $day->format('w'); $i < $j; $i++) {
                $this->days[] = array('type' => 'blank');
            }
        }
    }

    public function html($opts = array())
    {
        if (!isset($opts['id'])) {
            $opts['id'] = 'calendar';
        }
        if (!isset($opts['month_link'])) {
            $opts['month_link'] = '<a href="' . htmlentities($_SERVER['PHP_SELF']) . '?' . 'month=%d&amp;year=%d">%s</a>';
        }
        $classes = array();
        foreach (array('prev', 'month', 'next', 'weekday', 'blank', 'day', 'today') as $class) {
            if (isset($opts['class']) && isset($opts['class'][$class])) {
                $classes[$class] = $opts['class'][$class];
            } else {
                $classes[$class] = $class;
            }
        }

        $prevMonth = clone $this->monthToUse;
        $prevMonth->modify('-1 month');
        $prevMonthLink = sprintf(
            $opts['month_link'], $prevMonth->format('m'),
            $prevMonth->format("Y"),
            '&laquo'
        );

        $nextMonth = clone $this->monthToUse;
        $nextMonth->modify('+1 month');
        $nextMonthLink = sprintf(
            $opts['month_link'],
            $nextMonth->format('m'),
            $nextMonth->format('Y'),
            '&raquo'
        );

        $html = '<table id="' . htmlentities($opts['id']) . '">    
            <tr>
                <td class="' . htmlentities($classes['prev']) . '">
                ' . $prevMonthLink . '
                </td>
                <td class="' . htmlentities($classes['month']) . '" colspan="5">'
            . $this->monthToUse->format('F Y') .
            '</td>
            <td class="' . htmlentities($classes['next']) . '">'
            . $nextMonthLink .
            '</td>
            
            </tr>
        
        ';

        $html .= '<tr>';

        $lastDayIndex = count($this->days) - 1;


        foreach ($this->days as $i => $day) {
            switch ($day['type']) {
                case 'dow':
                    $class = 'weekday';
                    $label = htmlentities($day['label']);
                    break;
                case 'blank':
                    $class = 'blank';
                    $label = '&nbsp;';
                    break;
                case 'day':
                    $class = $day['today'] ? 'today' : 'day';
                    $label = htmlentities($day['label']);
                    break;
            }
            $html .= '<td class="' . htmlentities($classes[$class]) . '">' . $label . '</td>';

            if (isset($day['endOfRow']) && $day['endOfRow']) {
                $html .= '</tr>';
                if ($i != $lastDayIndex) {
                    $html .= '<tr>';
                }
            }

        }
        $html .= '</table>';
        return $html;
    }

    public function text()
    {
        $lineLength = strlen('Su Mo Tu We Th Fr Sa');
        $header = $this->monthToUse->format('F Y');
        $headerSpacing = floor(($lineLength - strlen($header)) / 2);

        $text = str_repeat(' ', $headerSpacing) . $header . "\n";

        foreach ($this->days as $i => $day) {
            switch ($day['type']) {
                case 'dow':
                    $text .= sprintf('% 2s', $day['label']);
                    break;
                case 'blank':
                    $text .= '';
                    break;
                case 'day':
                    $text .= sprintf('% 2d', $day['label']);
                    break;
            }
            $text .= (isset($day['endOfRow']) && $day['endOfRow']) ? '\n' : "";
        }
        if ($text[strlen($text) - 1] != '\n') {
            $text .= '\n';
        }
        return $text;
    }
}


$month = isset($_GET['month']) ? intval($_GET['month']) : date('m');

$year = isset($_GET['year']) ? intval($_GET['year']) : date('Y');

$cal = new LittleCalendar($month, $year);

print $cal->html();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值