给定两个日期在PHP中找到平日数量的最佳方式是什么? [英] Given two dates what is the best way of finding the number of weekdays in PHP?

查看:177
本文介绍了给定两个日期在PHP中找到平日数量的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎是自我解释的。给定两个日期什么是使用PHP查找星期几的最佳方法?星期一到星期五。



例如,我如何才能发现,在$ code> 31/08/2008之间有10个星期的日子? / code>和 13/09/2008

解决方案

  $ datefrom = strtotime($ datefrom,0); 
$ dateto = strtotime($ dateto,0);

$ difference = $ dateto - $ datefrom;

$ days_difference = floor($ difference / 86400);
$ weeks_difference = floor($ days_difference / 7); //完成周

$ first_day = date(w,$ datefrom);
$ days_remainder = floor($ days_difference%7);

$ odd_days = $ first_day + $ days_remainder; //剩下的是星期六还是星期天?
if($ odd_days> 7){//星期日
$ days_remainder--;
}
if($ odd_days> 6){//星期六
$ days_remainder--;
}

$ datediff =($ weeks_difference * 5)+ $ days_remainder;

从这里: http://www.addedbytes.com/php/php-datediff-function/


The title is pretty much self explanatory. Given two dates what is the best way of finding the number of week days using PHP? Week days being Monday to Friday.

For instance, how would I find out that there are 10 week days in between 31/08/2008 and 13/09/2008?

解决方案

        $datefrom = strtotime($datefrom, 0);
        $dateto = strtotime($dateto, 0);

        $difference = $dateto - $datefrom;

        $days_difference = floor($difference / 86400);
        $weeks_difference = floor($days_difference / 7); // Complete weeks

        $first_day = date("w", $datefrom);
        $days_remainder = floor($days_difference % 7);

        $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
        if ($odd_days > 7) { // Sunday
            $days_remainder--;
        }
        if ($odd_days > 6) { // Saturday
            $days_remainder--;
        }

        $datediff = ($weeks_difference * 5) + $days_remainder;

From here: http://www.addedbytes.com/php/php-datediff-function/

这篇关于给定两个日期在PHP中找到平日数量的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆