w35I3y的答案几乎是正确的,但我在使用该功能时遇到错误.此函数可以正确计算星期一的数量或两个给定日期之间的任何特定日期:
/**
* Counts the number occurrences of a certain day of the week between a start and end date
* The $start and $end variables must be in UTC format or you will get the wrong number
* of days when crossing daylight savings time
* @param - $day - the day of the week such as "Monday", "Tuesday"...
* @param - $start - a UTC timestamp representing the start date
* @param - $end - a UTC timestamp representing the end date
* @return Number of occurences of $day between $start and $end
*/
function countDays($day, $start, $end)
{
//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 floor( ( $end-$start )/60/60/24/7) + $partialWeekCount;
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
$start = strtotime("tuesday UTC");
$end = strtotime("3 tuesday UTC");
echo date("m/d/Y", $start). " - ".date("m/d/Y", $end). " has ". countDays(0, $start, $end). " Sundays";
Run Code Online (Sandbox Code Playgroud)
输出类似于:09/28/2010 - 2010年10月19日有3个星期日.
您可以创建一个递归使用strtotime()来计算天数的函数.既然strtotime("next monday");工作得很好.
function daycount($day, $startdate, $counter)
{
if($startdate >= time())
{
return $counter;
}
else
{
return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
}
}
echo daycount("monday", strtotime("01.01.2009"), 0);
Run Code Online (Sandbox Code Playgroud)
希望这是你正在寻找的东西:)
没有循环,没有递归
<?php
define('ONE_WEEK', 604800); // 7 * 24 * 60 * 60
function number_of_days($days, $start, $end) {
$w = array(date('w', $start), date('w', $end));
$x = floor(($end-$start)/ONE_WEEK);
$sum = 0;
for ($day = 0;$day < 7;++$day) {
if ($days & pow(2, $day)) {
$sum += $x + ($w[0] > $w[1]?$w[0] <= $day || $day <= $w[1] : $w[0] <= $day && $day <= $w[1]);
}
}
return $sum;
}
//$start = $end = time();
// 0x10 == pow(2, 4) == 1 << 4 // THURSDAY
// 0x20 == pow(2, 5) == 1 << 5 // FRIDAY
echo number_of_days(0x01, $start, $end); // SUNDAY
echo number_of_days(pow(2, 0), $start, $end); // SUNDAY
echo number_of_days(0x02, $start, $end); // MONDAY
echo number_of_days(pow(2, 1), $start, $end); // MONDAY
echo number_of_days(0x04, $start, $end); // TUESDAY
echo number_of_days(1 << 2, $start, $end); // TUESDAY
echo number_of_days(0x08, $start, $end); // WEDNESDAY
echo number_of_days(1 << 3, $start, $end); // WEDNESDAY
echo number_of_days(0x10, $start, $end); // THURSDAY
echo number_of_days(0x20, $start, $end); // FRIDAY
echo number_of_days(0x40, $start, $end); // SATURDAY
echo number_of_days(0x01 | 0x40, $start, $end); // WEEKENDS : SUNDAY | SATURDAY
echo number_of_days(0x3E, $start, $end); // WORKDAYS : MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY
?>
Run Code Online (Sandbox Code Playgroud)
这个问题只是迫切需要使用PHP的DateTime类的更新答案,所以这里是: -
/**
* @param String $dayName eg 'Mon', 'Tue' etc
* @param DateTimeInterface $start
* @param DateTimeInterface $end
* @return int
*/
function countDaysByName($dayName, \DateTimeInterface $start, \DateTimeInterface $end)
{
$count = 0;
$interval = new \DateInterval('P1D');
$period = new \DatePeriod($start, $interval, $end);
foreach($period as $day){
if($day->format('D') === ucfirst(substr($dayName, 0, 3))){
$count ++;
}
}
return $count;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14808 次 |
| 最近记录: |