【问题标题】:How can I calculate date weekend in php如何在php中计算日期周末
【发布时间】:2016-05-02 17:43:57
【问题描述】:

我有这样的约会

    $start = strtotime('2010-01-01'); $end = strtotime('2010-01-25');

我的问题:

如何从 $start 和 $end 日期范围计算或计算周末..??

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    更现代的方法是使用 php 的 DateTime 类。下面,您将获得一个以周数为键的数组。我添加了周数和周末天数。

    <?php
    
    $begin = new DateTime('2010-01-01');
    $end = new DateTime('2010-01-25');
    
    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval, $end);
    $weekends = [];
    
    foreach($daterange as $date) {
        if (in_array($date->format('N'), [6,7])) {
            $weekends[$date->format('W')][] = $date->format('Y-m-d');
        }
    }
    
    print_r($weekends);
    
    echo 'Number of weeks: ' . count($weekends);
    
    echo 'Number of weekend days: ' . (count($weekends, COUNT_RECURSIVE) - count($weekends));
    

    注意:如果您使用的是 PHP 5.3,请使用 array() 而不是块数组 []

    【讨论】:

    • hy Schellingerht 谢谢你的回答,但我对你的代码有疑问 new DateInterval('P1D');
    • DateInterval 是范围内的步数。 P1D 表示每步 1 天。因为你之前不知道开始日的天数是多少。在 foreach 循环中,每天都从头到尾经过。然后,我检查天数是 6(星期六)还是 7(星期日)。
    • 嗨@bd4dQuetions。好的!请为有相同问题的人标记正确的答案;-)
    【解决方案2】:

    也许这段代码 sn-p 会有所帮助:

    <?php
        //get current month for example
        $beginday = date("Y-m-01");
        $lastday  = date("Y-m-t");
    
        $nr_work_days = getWorkingDays($beginday, $lastday);
        echo $nr_work_days;
    
        function getWorkingDays($startDate, $endDate)
        {
            $begin = strtotime($startDate);
            $end   = strtotime($endDate);
            if ($begin > $end) {
                echo "startdate is in the future! <br />";
    
                return 0;
            } else {
                $no_days  = 0;
                $weekends = 0;
                while ($begin <= $end) {
                    $no_days++; // no of days in the given interval
                    $what_day = date("N", $begin);
                    if ($what_day > 5) { // 6 and 7 are weekend days
                        $weekends++;
                    };
                    $begin += 86400; // +1 day
                };
                $working_days = $no_days - $weekends;
    
                return $working_days;
            }
        }
    

    另一种解决方案可以是:(Get date range between two dates excluding weekends)

    【讨论】:

    【解决方案3】:

    这可能会有所帮助:

    $start = strtotime('2010-01-01'); 
    $end = strtotime('2010-01-25');
    
    $differ = $end-$start;
    
    $min = $differ/60;
    $hrs = $min/60;
    $days = $hrs/24;
    $weeks = $days/7;
    
    
    if(is_int($weeks))
    $weeks++;
    
    echo '<pre>';
    print_r(ceil($weeks));
    echo '</pre>';
    

    【讨论】:

    • hy, pritesh 我已经尝试使用你的代码,但对我不起作用我从日期开始尝试 $start = strtotime('2016-01-01'); $end = strtotime('2016-01-08'); 并且结果必须是 2 而不是 1
    • 好吧,所以你不想计算周末,但一个月的周末数是多少……我说得对吗??
    • Hy pritesh yah 一个月的周末数
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2020-09-04
    • 2022-12-12
    • 2015-02-03
    • 1970-01-01
    • 2018-07-20
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多