日历 - 下一个链接

发布于 2024-12-12 05:37:51 字数 171 浏览 0 评论 0原文

所以我目前使用这个 http://davidwalsh.name/php-calendar 作为我的日历,但我我无法想出一种方法来添加下个月的“下一个”/“上一个”链接......非常感谢任何帮助!

So I have currently used this http://davidwalsh.name/php-calendar as my calendar, but I am having trouble coming up with a way to add a 'next'/'previous' link to the next month... any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-12-19 05:37:51

由于绘制日历的功能是

function draw_calendar($month,$year){

您必须在下一个/上一个链接中提供 $month$year ,例如,

/calendar.php?month=12&year=2011

此数据可在 $_GET 中使用 当点击这样的链接时。您不想要未经处理的数据,因此您可以在日历脚本顶部像这样获取它:

$input = filter_input_array(
    INPUT_GET,
    array(
        'month' => array(
            'filter'    => FILTER_VALIDATE_INT,
            'options'   => array('min_range' => 1, 'max_range' => 12)
        ),
        'year' => array(
            'filter'    => FILTER_VALIDATE_INT,
            'options'   => array('min_range' => 2010, 'max_range' => 2015)
        )
    )
);

过滤器功能将确保我们获得 1 到 12 之间的一个月以及 2010 到 2015 之间的一年(相应调整或删除选项,如您所见合身)。如果传递的数字不在该范围内(或者尚未单击任何链接),我们将得到它们的 false ,这意味着我们必须设置合理的默认值,例如

$input['year']  = $input['year'] ?: date('Y');
$input['month'] = $input['month'] ?: date('n');

这将使用有效值传递给脚本,或者,如果值无效,则将年份和/或月份设置为当前年份和/或月份。

现在绘制日历:

echo draw_calendar($input['month'], $input['year']);

对于下一个/上一个链接,您可以手动检查月份是在 12 还是 1,然后相应地增加/减少年份,或者使用 DateTime 对象

$dateTime = new DateTime;
$dateTime->setDate($input['year'], $input['month'], 1));
printf(
    '<a href="/calendar.php?month=%d&year=%d">Next</a>' .
    '<a href="/calendar.php?month=%d&year=%d">Previous</a>',
    $dateTime->modify('-1 month')->format('n'),
    $dateTime->format('Y'),
    $dateTime->modify('+2 month')->format('n'),
    $dateTime->format('Y')
);

演示(略有删节)

另一种选择是将当前月份和年份存储在会话中,然后只有下一个/上一个链接没有年份和月份,而只是像 +1 和 -1 这样的东西来回移动。但是这样你就没有直接的方法可以跳转到某个月份。

这就是全部内容。

Since the function to draw the calender is

function draw_calendar($month,$year){

you have to supply $month and $year in the next/previous links, e.g.

/calendar.php?month=12&year=2011

This data is then available in $_GET when such a link is clicked. You dont want unsanitized data, so you fetch it like this on top of your calendar script:

$input = filter_input_array(
    INPUT_GET,
    array(
        'month' => array(
            'filter'    => FILTER_VALIDATE_INT,
            'options'   => array('min_range' => 1, 'max_range' => 12)
        ),
        'year' => array(
            'filter'    => FILTER_VALIDATE_INT,
            'options'   => array('min_range' => 2010, 'max_range' => 2015)
        )
    )
);

The filter function will make sure we get a month between 1 and 12 and a year between 2010 and 2015 (adjust accordingly or remove the options as you see fit). If the passed numbers are not in that range (or no link was clicked yet), we will get false for them, which means we will have to set sane defaults, e.g.

$input['year']  = $input['year'] ?: date('Y');
$input['month'] = $input['month'] ?: date('n');

This will either use the valid values passed to the script or, in case of invalid values, set the year and/or month to the current year and/or month.

Now draw the calendar:

echo draw_calendar($input['month'], $input['year']);

For the next/previous link you can either manually check whether the month is at 12 or 1 and then increase/decrease the year accordingly or use a DateTime object

$dateTime = new DateTime;
$dateTime->setDate($input['year'], $input['month'], 1));
printf(
    '<a href="/calendar.php?month=%d&year=%d">Next</a>' .
    '<a href="/calendar.php?month=%d&year=%d">Previous</a>',
    $dateTime->modify('-1 month')->format('n'),
    $dateTime->format('Y'),
    $dateTime->modify('+2 month')->format('n'),
    $dateTime->format('Y')
);

demo (slightly abridged)

Another option would be to store the current month and year in a session and then just have next/previous links without year and month but rather just something like +1 and -1 to go back and forth. But then you have no direct way to jump to a certain month.

And that's all there is to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文