查找两个日期之间的特定日期的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")