查找两个日期之间的特定日期的PHP PHP不使用任何循环? [英] find cout of specific day between two date php without using any loop?

查看:65
本文介绍了查找两个日期之间的特定日期的PHP PHP不使用任何循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出两个日期之间特定日期的计数。
我可以通过使用两个日期之间的循环来做到这一点,但是如果两个日期之间存在10年的差异(假设),则循环将运行10 * 365次。
有什么更简单的方法吗?

预先感谢

I need to find out count of a specific Day between two date. I can do this by using loop between two date, but if there is difference of 10 years(suppose) between date loop will run 10*365 times. Is any easier way to do this?
Thanks in advance

推荐答案

function countDays( $ day,$ start,$ end)
{

function countDays($day, $start, $end) {

$start = strtotime($start);
$end = strtotime($end);
$datediff = $end - $start;

$days =  floor($datediff / (60 * 60 * 24));


//get the day of the week for start and end dates (0-6)
$w = array( date('w', $start ), date('w', $end) );

//get partial week day count
if ($w[0] < $w[1])
{            
    $partialWeekCount = ($day >= $w[0] && $day <= $w[1]);
}else if ($w[0] == $w[1])
{
    $partialWeekCount = $w[0] == $day;
}else
{
    $partialWeekCount = ($day >= $w[0] 
    $day <= $w[1]); 
} 
//first count the number of complete weeks, then add 1 if $day falls in a partial week. 
return intval($days / 7) + $partialWeekCount; 

}

函数调用-countDays( 5, 2017-04-14, 2017-04-21)

Function Call - countDays( 5, "2017-04-14", "2017-04-21")

这篇关于查找两个日期之间的特定日期的PHP PHP不使用任何循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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