计算星期六和星期日的数量

时间:2022-11-30 09:26:42

How can I calculate the number of Saturdays and Sundays between two dates in php?

如何在php中计算两个日期之间的星期六和星期日的数量?

Is there any inbuilt function for that purpose?

是否有任何内置功能用于此目的?

6 个解决方案

#1


5  

There is a related question here already, Calculate business days

这里有一个相关的问题,计算工作日

You can use this to subtract from 7 to get the weekend days, or similar.

您可以使用它从7中减去周末天数或类似天数。

#2


4  

I don't think there is a built in for that, but this should do the job :

我不认为有内置的,但这应该做的工作:

$startTime = START_TIMESTAMP;
$endTime = END_TIMESTAMP;
$time = $startTime;
$count = 0;

while(date('w', $time) != 0) { // 0 (for Sunday) through 6 (for Saturday)
    $time += 86400;
}

while($time < $endTime) {
    $count++;
    $time += 7 * 86400;
}

#3


1  

Let us all KISS (Keep It Simple Stupid). Why make it so complicated?

让我们所有的吻(保持简单愚蠢)。为什么要这么复杂?

function countWeekendDays($start, $end)
{
    // $start in timestamp
        // $end in timestamp


    $iter = 24*60*60; // whole day in seconds
    $count = 0; // keep a count of Sats & Suns

    for($i = $start; $i <= $end; $i=$i+$iter)
    {
        if(Date('D',$i) == 'Sat' || Date('D',$i) == 'Sun')
        {
            $count++;
        }
    }
    return $count;
   }

#4


0  

<?php
date_default_timezone_set("Europe/Lisbon");
$d1 = new DateTime("2009-06-01"); /* inclusive */
$d2 = new DateTime("2009-07-01"); /* exclusive */

$interval = $d2->diff($d1);
$number_of_days = $interval->format("%d");

$number_of_weekends = $number_of_days / 7;
$remainder = $number_of_days % 7;

if ($remainder >=2 && $d1->format("D") == "Sat")
    $number_of_weekends++;
elseif ($d1->format("w") + $remainder >= 8)
    $number_of_weekends++;

I may have missed by one in the last condition, be sure to check it with different starting dates. (Feel free to edit this answer if you spot an error).

我可能在最后一个条件中错过了一个,一定要用不同的开始日期检查它。 (如果发现错误,请随意编辑此答案)。

#5


0  

there's definitely no built in function for that but you can use strtotime to loop days

肯定没有内置功能,但你可以使用strtotime循环天

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

function numWeekdays( $start_ts, $end_ts, $day, $include_start_end = false ) {

    $day = strtolower( $day );
    $current_ts = $start_ts;
    // loop next $day until timestamp past $end_ts
    while( $current_ts < $end_ts ) {

        if( ( $current_ts = strtotime( 'next '.$day, $current_ts ) ) < $end_ts) {
            $days++;
        }
    }

    // include start/end days
    if ( $include_start_end ) {
        if ( strtolower( date( 'l', $start_ts ) ) == $day ) {
            $days++;
        }
        if ( strtolower( date( 'l', $end_ts ) ) == $day ) {
            $days++;
        }
    }   

    return (int)$days;

}

echo numWeekDays( $start, $end, 'saturday', false );

#6


0  

I searched for awhile for a simple solution that worked and decided to write my own and came up with this

我搜索了一段时间,找到了一个简单的解决方案,并且决定自己编写并提出这个问题

        $start = date('Y-m-d');
        $end = date('Y-m-d', strtotime($start.' +1 year'));
        $current = $start;
        $count = 0;

        while($current != $end){
            if(date('l', strtotime($current)) == 'Saturday'){
                $count++;
            }

            $current = date('Y-m-d', strtotime($current.' +1 day'));
        };

        echo $count;

#1


5  

There is a related question here already, Calculate business days

这里有一个相关的问题,计算工作日

You can use this to subtract from 7 to get the weekend days, or similar.

您可以使用它从7中减去周末天数或类似天数。

#2


4  

I don't think there is a built in for that, but this should do the job :

我不认为有内置的,但这应该做的工作:

$startTime = START_TIMESTAMP;
$endTime = END_TIMESTAMP;
$time = $startTime;
$count = 0;

while(date('w', $time) != 0) { // 0 (for Sunday) through 6 (for Saturday)
    $time += 86400;
}

while($time < $endTime) {
    $count++;
    $time += 7 * 86400;
}

#3


1  

Let us all KISS (Keep It Simple Stupid). Why make it so complicated?

让我们所有的吻(保持简单愚蠢)。为什么要这么复杂?

function countWeekendDays($start, $end)
{
    // $start in timestamp
        // $end in timestamp


    $iter = 24*60*60; // whole day in seconds
    $count = 0; // keep a count of Sats & Suns

    for($i = $start; $i <= $end; $i=$i+$iter)
    {
        if(Date('D',$i) == 'Sat' || Date('D',$i) == 'Sun')
        {
            $count++;
        }
    }
    return $count;
   }

#4


0  

<?php
date_default_timezone_set("Europe/Lisbon");
$d1 = new DateTime("2009-06-01"); /* inclusive */
$d2 = new DateTime("2009-07-01"); /* exclusive */

$interval = $d2->diff($d1);
$number_of_days = $interval->format("%d");

$number_of_weekends = $number_of_days / 7;
$remainder = $number_of_days % 7;

if ($remainder >=2 && $d1->format("D") == "Sat")
    $number_of_weekends++;
elseif ($d1->format("w") + $remainder >= 8)
    $number_of_weekends++;

I may have missed by one in the last condition, be sure to check it with different starting dates. (Feel free to edit this answer if you spot an error).

我可能在最后一个条件中错过了一个,一定要用不同的开始日期检查它。 (如果发现错误,请随意编辑此答案)。

#5


0  

there's definitely no built in function for that but you can use strtotime to loop days

肯定没有内置功能,但你可以使用strtotime循环天

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

function numWeekdays( $start_ts, $end_ts, $day, $include_start_end = false ) {

    $day = strtolower( $day );
    $current_ts = $start_ts;
    // loop next $day until timestamp past $end_ts
    while( $current_ts < $end_ts ) {

        if( ( $current_ts = strtotime( 'next '.$day, $current_ts ) ) < $end_ts) {
            $days++;
        }
    }

    // include start/end days
    if ( $include_start_end ) {
        if ( strtolower( date( 'l', $start_ts ) ) == $day ) {
            $days++;
        }
        if ( strtolower( date( 'l', $end_ts ) ) == $day ) {
            $days++;
        }
    }   

    return (int)$days;

}

echo numWeekDays( $start, $end, 'saturday', false );

#6


0  

I searched for awhile for a simple solution that worked and decided to write my own and came up with this

我搜索了一段时间,找到了一个简单的解决方案,并且决定自己编写并提出这个问题

        $start = date('Y-m-d');
        $end = date('Y-m-d', strtotime($start.' +1 year'));
        $current = $start;
        $count = 0;

        while($current != $end){
            if(date('l', strtotime($current)) == 'Saturday'){
                $count++;
            }

            $current = date('Y-m-d', strtotime($current.' +1 day'));
        };

        echo $count;