【问题标题】:Array of working hours from 9:00 to 18:00从 9:00 到 18:00 的工作时间数组
【发布时间】:2018-06-20 12:57:07
【问题描述】:

我需要将工作时间报告保存到数据库中。工作时间为 9:00 至 18:00

  • 我有开始日期:2018/06/01 13:00:00

  • 结束日期:2018/06/04 17:00:00

结果应该是每个工作日的工作时间的报告数组:

  1. 6 月 1 日:签到 - 13:00;结帐18:00
    --跳过两个周末--
  2. 6 月 4 日:签到 - 09:00;结帐18:00
  3. 6 月 5 日:签到09:00;结帐18:00
  4. 6 月 6 日:签到9:00;结帐17:00

知道怎么做吗?

编辑:不要误会我的意思,我不希望任何人为我编写代码 :) 我只需要一个提示来了解如何让循环知道每天何时开始和何时结束。这应该是提供的工作日的每个小时的循环吗?

【问题讨论】:

  • 听起来你有一个想法,所以去做吧。我们不会为您编写代码,但如果您遇到错误或其他问题,请发布您的代码,我们可以为您提供帮助。
  • 问题是我不知道从哪里开始,循环应该如何工作。我至少需要一个开始,或者对它背后的逻辑做一个简单的解释。
  • @PerSeM 这真的不是 stackoverflow 的工作方式,它不是您讨论问题并解决问题的学校或论坛,而是针对特定编程问题的问答网站。
  • 好吧,如果你想开始一些事情: 1/ 首先创建一个函数,根据一小时开始和一小时结束给你所有工作时间(你有一些函数可以将 1h 添加到例如日期,所以只需在开始时添加 1h 而
  • @MickaelLeger 基本上你的意思是我应该每小时循环一次,如果总小时超过 18:00,我应该跳到另一天?我相信应该有一个更有效的公式。但如果我找不到任何东西,我会使用你的建议。

标签: php time timestamp


【解决方案1】:

首先,您需要使用一些 php 函数(如 date() 或 Datetime() 类)循环遍历每一天。然后(在循环中)您需要将日期与相应的工作时间添加到数组中,但始终检查这一天是否是周末(您可以使用上述函数或类来实现这一点)。循环之后,将数组插入数据库。

【讨论】:

  • 我如何知道“各个小时”?这是我的实际问题。我可以为每一天做一个循环,很容易计算有多少天,但是我怎么知道我每天应该投入多少小时,第一次报告和最后一次报告的开始时间是什么时候,以及如何如果报告仅在 1 天内仅持续几个小时,代码会运行吗?
【解决方案2】:

所以,我终于想通了。我会把它贴在这里,以防有人从中找到灵感。

    function get_working_days($start_time = "", $end_time = "") {

        //getting the difference between $start_time and $end_time in days
        $diff = ceil(($end_time - $start_time) / 60 / 60 / 24);

        // fixing the difference if the request is for 1 day only
        if (($end_time - $start_time) > 32400 && ($end_time - $start_time) < 86400) {
            $diff += 1;
        }

        // 9:00 of the current day
        $day_start = strtotime("midnight", $start_time) + 32400;

        // 18:00 of the current day
        $day_end = strtotime("midnight", $end_time) + 64800;

        // setting the current day
        $curr_day = $day_start;

        // initiating the array
        $return = [];

        // looping through each day using the $diff variable
        for ($i=0; $i < $diff; $i++) {

            // adding reports while skipping weekends
            if (!is_weekend($curr_day)) {
                if ($i == 0 && $diff <= 1) { 
                    // first and only iteration
                    $return[] = array("checkin" => $start_time, "checkout" => $end_time);
                }elseif ($i == 0 && $i < ($diff - 1)) { 
                    // first but not only iteration
                    $return[] = array("checkin" => $start_time, "checkout" => $curr_day + 32400);
                }elseif ($i < ($diff - 1)) { 
                    // middle 
                    $return[] = array("checkin" => $curr_day, "checkout" => $curr_day + 32400);
                }else{ 
                    // end
                    $return[] = array("checkin" => $curr_day, "checkout" => $end_time);
                }
            }
            // going to the next day
            $curr_day += 86400;
        }

        return $return;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2013-02-07
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2015-03-11
    • 1970-01-01
    相关资源
    最近更新 更多