创建工作日数组

发布于 2024-10-14 05:46:10 字数 396 浏览 4 评论 0原文

我需要一种在 PHP 中创建 2 个日期之间的“工作日”数组的方法。

例如:

getWorkingDays("2008-01-01","2009-06-30");

将创建一个如下所示的数组:

Array
(
    [0] ="2008-01-01",
    [1] ="2008-01-05",
    [2] ="2008-01-06",
    [3] ="2008-01-07",
    [4] ="2008-01-08",
    [5] ="2008-01-09",
    [6] ="2008-01-12",
    [7] ="2008-01-13",
    [8] ="2008-01-14",
    ...
) 

I need a method for creating an array of "business days" in PHP beteewn 2 dates.

For example:

getWorkingDays("2008-01-01","2009-06-30");

Will create an array like the following:

Array
(
    [0] ="2008-01-01",
    [1] ="2008-01-05",
    [2] ="2008-01-06",
    [3] ="2008-01-07",
    [4] ="2008-01-08",
    [5] ="2008-01-09",
    [6] ="2008-01-12",
    [7] ="2008-01-13",
    [8] ="2008-01-14",
    ...
) 

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

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

发布评论

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

评论(2

浮世清欢 2024-10-21 05:46:10

最简单的技术是创建一个从开始日期到结束日期的循环(您应该通过 mktime,然后添加一天(86400 秒)作为增量)。在循环的内部,您将每个日期转换为 unix 时间戳(再次通过 mktime 等),然后使用...

date('N', $dayStamp)

...获取相关日期是星期几。

作为基本实现,这看起来像:

<?php

    function getWorkingDays($startDate, $endDate) {

        $businessDays = array();
        $businessDaysInWeek = range(1,5);      // Only Monday to Friday

        // Decompose the provided dates.        
        list($startYear, $startMonth, $startDay) = explode('-', $startDate);
        list($endYear, $endMonth, $endDay) = explode('-', $endDate);

        // Create our start and end timestamps.
        $startStamp = mktime(1, 1, 1, $startMonth, $startDay, $startYear);
        $endStamp = mktime(1, 1, 1, $endMonth, $endDay, $endYear);

        // Check each day in turn.
        for($loop=$startStamp; $loop<=$endStamp; $loop+=86400) {
            if(in_array(date('N', $loop), $businessDaysInWeek)) {

                // You'll also want to omit bank holidays, etc. in here.

                $businessDays[] = date('Y-m-d', $loop);
            }
        }

        return $businessDays;
    }

    print_r(getWorkingDays('2011-01-10', '2011-01-24'));

?>

但是,您很可能还需要省略银行假日等,因此您应该将此类异常存储在数据库表/查找数组等中,如下所示酌情检查它们。

顺便说一句,如果您使用的 PHP 版本早于 5.1.0,则应使用 date('w'... 并相应地调整星期几。(它从零开始计数,而不是从零开始计数)一。)

The simplest technique would be to create a loop from the starting date until the end date (you should covert both to timestamps via mktime and then add a day (86400 seconds) to increment). In the inner part of the loop, you'd convert each date into a unix timestamp (once again via mktime, etc.) and then use...

date('N', $dayStamp)

...to get the day of the week for the date in question.

As a basic implemention, this would look like:

<?php

    function getWorkingDays($startDate, $endDate) {

        $businessDays = array();
        $businessDaysInWeek = range(1,5);      // Only Monday to Friday

        // Decompose the provided dates.        
        list($startYear, $startMonth, $startDay) = explode('-', $startDate);
        list($endYear, $endMonth, $endDay) = explode('-', $endDate);

        // Create our start and end timestamps.
        $startStamp = mktime(1, 1, 1, $startMonth, $startDay, $startYear);
        $endStamp = mktime(1, 1, 1, $endMonth, $endDay, $endYear);

        // Check each day in turn.
        for($loop=$startStamp; $loop<=$endStamp; $loop+=86400) {
            if(in_array(date('N', $loop), $businessDaysInWeek)) {

                // You'll also want to omit bank holidays, etc. in here.

                $businessDays[] = date('Y-m-d', $loop);
            }
        }

        return $businessDays;
    }

    print_r(getWorkingDays('2011-01-10', '2011-01-24'));

?>

However, you'll most likely also need to omit bank holidays, etc. so you should store such exceptions in a database table/lookup array, etc. as check against them as appropriate.

Incidentally, if you're using a version of PHP earlier than 5.1.0, you should use date('w'... and adjust your days of week accordingly. (It counts from zero instead of one.)

椒妓 2024-10-21 05:46:10

有了基础的 php 日期函数,你就可以做到这一点,花点时间阅读文档 PHP 日期

With the basics php date function you can make it, take the time to read the documentation at PHP date

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