在PHP上显示日历

时间:2012-04-25 03:47:34

标签: php html calendar

我一直在寻找PHP上的日历,但我搜索的都是日期时间选择器。

但我唯一想要的是一个简单的日历,它会向我显示日期,而不会选择日期。

我只需要显示日历,就像普通日历在我们的操作系统中的工作方式一样,谢谢。

有什么办法吗?

3 个答案:

答案 0 :(得分:8)

您可以使用以下内容:http://keithdevens.com/software/php_calendar

<?php
//http://keithdevens.com/software/php_calendar
$time = time();
$today = date('j', $time);
$days = array($today => array(null, null,'<div id="today">' . $today . '</div>'));
$pn = array('&laquo;' => date('n', $time) - 1, '&raquo;' => date('n', $time) + 1);
echo generate_calendar(date('Y', $time), date('n', $time), $days, 1, null, 0);

// PHP Calendar (version 2 . 3), written by Keith Devens
// http://keithdevens . com/software/php_calendar
//  see example at http://keithdevens . com/weblog
// License: http://keithdevens . com/software/license

function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array())
{
    $first_of_month = gmmktime(0, 0, 0, $month, 1, $year);
    // remember that mktime will automatically correct if invalid dates are entered
    // for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
    // this provides a built in "rounding" feature to generate_calendar()

    $day_names = array(); //generate all the day names according to the current locale
    for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t+=86400) //January 4, 1970 was a Sunday
        $day_names[$n] = ucfirst(gmstrftime('%A', $t)); //%A means full textual day name

    list($month, $year, $month_name, $weekday) = explode(',', gmstrftime('%m, %Y, %B, %w', $first_of_month));
    $weekday = ($weekday + 7 - $first_day) % 7; //adjust for $first_day
    $title   = htmlentities(ucfirst($month_name)) . $year;  //note that some locales don't capitalize month and day names

    //Begin calendar .  Uses a real <caption> .  See http://diveintomark . org/archives/2002/07/03
    @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); //previous and next links, if applicable
    if($p) $p = '<span class="calendar-prev">' . ($pl ? '<a href="' . htmlspecialchars($pl) . '">' . $p . '</a>' : $p) . '</span>&nbsp;';
    if($n) $n = '&nbsp;<span class="calendar-next">' . ($nl ? '<a href="' . htmlspecialchars($nl) . '">' . $n . '</a>' : $n) . '</span>';
    $calendar = "<div class=\"mini_calendar\">\n<table>" . "\n" . 
        '<caption class="calendar-month">' . $p . ($month_href ? '<a href="' . htmlspecialchars($month_href) . '">' . $title . '</a>' : $title) . $n . "</caption>\n<tr>";

    if($day_name_length)
    {   //if the day names should be shown ($day_name_length > 0)
        //if day_name_length is >3, the full name of the day will be printed
        foreach($day_names as $d)
            $calendar  .= '<th abbr="' . htmlentities($d) . '">' . htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d) . '</th>';
        $calendar  .= "</tr>\n<tr>";
    }

    if($weekday > 0) 
    {
        for ($i = 0; $i < $weekday; $i++) 
        {
            $calendar  .= '<td>&nbsp;</td>'; //initial 'empty' days
        }
    }
    for($day = 1, $days_in_month = gmdate('t',$first_of_month); $day <= $days_in_month; $day++, $weekday++)
    {
        if($weekday == 7)
        {
            $weekday   = 0; //start a new week
            $calendar  .= "</tr>\n<tr>";
        }
        if(isset($days[$day]) and is_array($days[$day]))
        {
            @list($link, $classes, $content) = $days[$day];
            if(is_null($content))  $content  = $day;
            $calendar  .= '<td' . ($classes ? ' class="' . htmlspecialchars($classes) . '">' : '>') . 
                ($link ? '<a href="' . htmlspecialchars($link) . '">' . $content . '</a>' : $content) . '</td>';
        }
        else $calendar  .= "<td>$day</td>";
    }
    if($weekday != 7) $calendar  .= '<td id="emptydays" colspan="' . (7-$weekday) . '">&nbsp;</td>'; //remaining "empty" days

    return $calendar . "</tr>\n</table>\n</div>\n";
}
?>

这是一个非常基本的迷你日历。没有奇特的功能。

答案 1 :(得分:5)

我用PHP5 + ajax编写了一个这样的代码(如果你需要使用它):

<?php

class PN_Calendar {

    public $first_day_of_week = 0; //0 - sunday, 1 - monday
    public $current_year = null;
    public $current_month = null;
    public $current_day = null;
    public $show_year_selector = true;
    public $min_select_year = 1991;
    public $max_select_year = 2045;
    public $show_month_selector = true;

    public function __construct($atts = array()) {
        if (isset($atts['first_day_of_week'])) {
            $this->first_day_of_week = $atts['first_day_of_week'];
        }

        if (!isset($atts['year'])) {
            $this->current_year = date('Y');
        } else {
            $this->current_year = $atts['year'];
        }

        if (!isset($atts['month'])) {
            $this->current_month = date('m');
        } else {
            $this->current_month = $atts['month'];
        }

        if (!isset($atts['day'])) {
            $this->current_day = date('d');
        } else {
            $this->current_day = $atts['day'];
        }
        //***
        if (isset($atts['show_year_selector'])) {
            $this->show_year_selector = $atts['show_year_selector'];
        }

        if (isset($atts['show_month_selector'])) {
            $this->show_month_selector = $atts['show_month_selector'];
        }

        if (isset($atts['min_select_year'])) {
            $this->min_select_year = $atts['min_select_year'];
        }

        if (isset($atts['max_select_year'])) {
            $this->max_select_year = $atts['max_select_year'];
        }
    }

    /*
     * Month calendar drawing
     */

    public function draw($data = array(), $y = 0, $m = 0) {
        //***
        if ($m == 0 AND $m == 0) {
            $y = $this->current_year;
            $m = $this->current_month;
        }
        //***
        $data['week_days_names'] = $this->get_week_days_names(true);
        $data['cells'] = $this->generate_calendar_cells($y, $m);
        $data['month_name'] = $this->get_month_name($m);
        $data['year'] = $y;
        $data['month'] = $m;
        return $this->draw_html('calendar', $data);
    }

    private function generate_calendar_cells($y, $m) {
        $y = intval($y);
        $m = intval($m);
        //***
        $first_week_day_in_month = date('w', mktime(0, 0, 0, $m, 1, $y)); //from 0 (sunday) to 6 (saturday)
        $days_count = $this->get_days_count_in_month($y, $m);
        $cells = array();
        //***
        if ($this->first_day_of_week == $first_week_day_in_month) {
            for ($d = 1; $d <= $days_count; $d++) {
                $cells[] = new PN_CalendarCell($y, $m, $d);
            }
            //***
            $cal_cells_left = 5 * 7 - $days_count;
            $next_month_data = $this->get_next_month($y, $m);
            for ($d = 1; $d <= $cal_cells_left; $d++) {
                $cells[] = new PN_CalendarCell($next_month_data['y'], $next_month_data['m'], $d, false);
            }
        } else {
            //***
            if ($this->first_day_of_week == 0) {
                $cal_cells_prev = 6 - (7 - $first_week_day_in_month); //checked, is right
            } else {
                if ($first_week_day_in_month == 1) {
                    $cal_cells_prev = 0;
                } else {
                    if ($first_week_day_in_month == 0) {
                        $cal_cells_prev = 6 - 1;
                    } else {
                        $cal_cells_prev = 6 - (7 - $first_week_day_in_month) - 1;
                    }
                }
            }
            //***
            $prev_month_data = $this->get_prev_month($y, $m);
            $prev_month_days_count = $this->get_days_count_in_month($prev_month_data['y'], $prev_month_data['m']);

            for ($d = $prev_month_days_count - $cal_cells_prev; $d <= $prev_month_days_count; $d++) {
                $cells[] = new PN_CalendarCell($prev_month_data['y'], $prev_month_data['m'], $d, false);
            }

            //***
            for ($d = 1; $d <= $days_count; $d++) {
                $cells[] = new PN_CalendarCell($y, $m, $d);
            }
            //***
            //35(7*5) or 42(7*6) cells
            $busy_cells = $cal_cells_prev + $days_count;
            $cal_cells_left = 0;
            if ($busy_cells < 35) {
                $cal_cells_left = 35 - $busy_cells - 1;
            } else {
                $cal_cells_left = 42 - $busy_cells - 1;
            }
            //***
            if ($cal_cells_left > 0) {
                $next_month_data = $this->get_next_month($y, $m);
                for ($d = 1; $d <= $cal_cells_left; $d++) {
                    $cells[] = new PN_CalendarCell($next_month_data['y'], $next_month_data['m'], $d, false);
                }
            }
        }
        //***
        return $cells;
    }

    public function get_next_month($y, $m) {
        $y = intval($y);
        $m = intval($m);

        //***
        $m++;
        if ($m % 13 == 0 OR $m > 12) {
            $y++;
            $m = 1;
        }

        return array('y' => $y, 'm' => $m);
    }

    public function get_prev_month($y, $m) {
        $y = intval($y);
        $m = intval($m);

        //***
        $m--;
        if ($m <= 0) {
            $y--;
            $m = 12;
        }

        return array('y' => $y, 'm' => $m);
    }

    public function get_days_count_in_month($year, $month) {
        return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
    }

    public static function get_month_name($m) {
        $names = self::get_monthes_names();
        return $names[intval($m)];
    }

    public function get_week_day_name($num, $shortly = false) {
        $names = $this->get_week_days_names($shortly);
        return $names[intval($num)];
    }

    public function get_week_days_names($shortly = false) {
        if ($this->first_day_of_week == 1) {
            if ($shortly) {
                return array(
                    1 => 'Mo',
                    2 => 'Tu',
                    3 => 'We',
                    4 => 'Th',
                    5 => 'Fr',
                    6 => 'Sa',
                    7 => 'Su'
                );
            }

            return array(
                1 => 'Monday',
                2 => 'Tuesday',
                3 => 'Wednesday',
                4 => 'Thursday',
                5 => 'Friday',
                6 => 'Saturday',
                7 => 'Sunday'
            );
        } else {
            if ($shortly) {
                return array(
                    0 => 'Su',
                    1 => 'Mo',
                    2 => 'Tu',
                    3 => 'We',
                    4 => 'Th',
                    5 => 'Fr',
                    6 => 'Sa'
                );
            }

            return array(
                0 => 'Sunday',
                1 => 'Monday',
                2 => 'Tuesday',
                3 => 'Wednesday',
                4 => 'Thursday',
                5 => 'Friday',
                6 => 'Saturday'
            );
        }
    }

    public static function get_monthes_names() {
        return array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December'
        );
    }

    public function draw_html($view, $data = array()) {
        @extract($data);
        ob_start();
        include('views/' . $view . '.php' );
        return ob_get_clean();
    }

    }


class PN_CalendarCell {

    public $cell_year = null;
    public $cell_month = null;
    public $cell_day = null;
    public $in_current_month = true;

    public function __construct($y, $m, $d, $in_current_month = true) {
        $this->cell_year = $y;
        $this->cell_month = $m;
        $this->cell_day = $d;
        $this->in_current_month = $in_current_month;
    }

    public function get_week_day_num() {
        return date('w', mktime(0, 0, 0, $this->cell_month, $this->cell_day, $this->cell_year)); //from 0 (sunday) to 6 (saturday);
    }

    public function draw($events) {
        $this_day_events = 0;
        if (is_array($events)) {
            if (isset($events[$this->cell_year][$this->cell_month][$this->cell_day])) {
                $this_day_events = count($events[$this->cell_year][$this->cell_month][$this->cell_day]);
            }
        } else {
            $events = array();
        }
        ?>

        <span class="pn_cal_cell_ev_counter" <?php if ($this_day_events <= 0): ?>style="display: none;"<?php endif; ?>><?php echo $this_day_events ?></span>
        <a data-year="<?php echo $this->cell_year ?>" data-month="<?php echo $this->cell_month ?>" data-day="<?php echo $this->cell_day ?>" data-week-day-num="<?php echo $this->get_week_day_num() ?>" href="javascript:void(0);" class="<?php if ($this->in_current_month): ?>pn_this_month<?php else: ?>other_month<?php endif; ?>"><?php echo $this->cell_day ?></a>
    }

}

脚本调用:

<?php
   include_once 'classes/calendar.php';
   $calendar = new PN_Calendar();
   echo $calendar->draw();
?>

使用演示下载脚本http://pluginus.net/archives/simple-free-open-source-ajax-php-calendar-script

演示http://2tests.pluginus.net/calendar/

答案 2 :(得分:0)

我也在寻找一个简单的解决方案,一个PHP类,它为我提供了一个月度日历,我可以轻松添加事件。

我找到了这个PHP日历http://coreyworrell.com/calendar/

您还必须在html中创建视图/模板,但您可以在那里找到可以使用的CSS和HTML版本。

它还有PHP中的自定义事件

$event = $calendar->event()
  ->condition('timestamp', strtotime('January 2, 2010'))
  ->title('Hello All')
  ->output('My Custom Event')
  ->add_class('custom-event-class');