计算给定月份和年份的星期日数

时间:2016-11-10 06:08:31

标签: php

我希望输出在给定的月份和年份中计算星期日。

这是我的代码:

$months=$_POST['month'];  
$years=$_POST['year'];                                      
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of  $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';

$num_sundays='';                
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt)) / 86400); $i++)
{
    if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday')
    {
            $num_sundays++;
    }
}

如果我回显$ num_sundays,我没有得到任何输出。请帮我 。我是PHP的新手

8 个答案:

答案 0 :(得分:4)

您只需要从这两行中删除<br>

$fromdt=date('Y-m-01 ',strtotime("First Day Of  $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';

否则,这将是开始日期和结束日期的一部分,您的strtotime()将返回false。

示例:

<?php
$months = 12;  
$years=2016;                                      
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of  $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';

var_dump(strtotime($todt));
var_dump(strtotime($fromdt));
?>

DEMO: 这将为两者返回false。

示例2:

<?php
$months = 12;  
$years=2016;                                      
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of  $monthName $years")) ;
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years"));

var_dump(strtotime($todt));
var_dump(strtotime($fromdt));
?>

DEMO: 这将返回值

完成示例:

<?php
$months = 12;  
$years=2016;                                      
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of  $monthName $years")) ;
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years"));

$num_sundays='';                
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt)) / 86400); $i++)
{
    if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday')
    {
            $num_sundays++;
    }    
}
echo "Total Count is: ".$num_sundays;
?>

DEMO: 这将返回4周日

答案 1 :(得分:1)

获取所有星期日的月份见下面的代码:

function total_sun($month,$year)
{
    $sundays=0;
    $total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
    for($i=1;$i<=$total_days;$i++)
    if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
    $sundays++;
    return $sundays;
}
echo total_sun(11,2016);

http://phpio.net/s/l9f

答案 2 :(得分:1)

无循环。我希望这能给出正确的结果。

date_default_timezone_set('UTC');

// unix timestamp 0 = Thursday, 01-Jan-70 00:00:00 UTC
// unix timestamp 259200 = Sunday, 04-Jan-70 00:00:00 UTC

$sun_first = strtotime('1970-01-04');

$t1 = strtotime('2018-10-01') - $sun_first - 86400;
$t2 = strtotime('2018-10-31') - $sun_first;

$sun_count = floor($t2 / 604800) - floor($t1 / 604800); // total Sunday from 2018-10-01 to 2018-10-31

echo $sun_count; // 4

答案 3 :(得分:0)

工作示例dorcode calculation

$startd="31-7-2006 15:30:00";
$endd="31-7-2007 15:30:00";

$startDate=$startd;
$endDate=$endd;
$startDate1 = strtotime($startDate);
$endDate1 = strtotime($endDate);
if($startDate1>$endDate1) 
{
$startDate1 = strtotime($endDate); 
$endDate1 = strtotime($startDate);
} else {
$startDate1 = strtotime($startDate);
$endDate1 = strtotime($endDate);
}
$p=0;
for($i = strtotime("Sunday", $startDate1); $i <= $endDate1;
 $i =strtotime('+1 week', $i))    
 {
$p++;
echo $p.": ".date('F d, Y', $i)."<br>";
}

答案 4 :(得分:0)

要获取一年中给定月份中任何给定日期的计数:

    $year = '2019';
    $month = '2';
    $day = 'Tuesday';
    $count = 0;
    $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);

    $date = new Datetime($year.'-'.$month.'-01');

    for($i=1; $i<$days; $i++){
        if($date->format('l') == $day){
            $count++;
        }
        $date->modify('+1 day');
    }
    echo "Count: $count";

答案 5 :(得分:0)

检查以下示例。我工作得很好。

function dayCount($day,$month,$year){
$totalDay=cal_days_in_month(CAL_GREGORIAN,$month,$year);

$count=0;

for($i=1;$totalDay>=$i;$i++){

  if( date('l', strtotime($year.'-'.$month.'-'.$i))==ucwords($day)){
    $count++;
    }

}

echo $count;


}


dayCount('saturday',3,2019);

答案 6 :(得分:0)

如果有帮助,请使用此代码

   public function countWeekendDays($month, $year){
        $daytime = strtotime(date($year."/".$month."/01 00:00:01"));
        $daysOfMonth = date("t", $daytime);
        $weekdays = 0;
        for ($day=1;  $day <= $daysOfMonth; $day++) { 
            $time = strtotime(date($year.'/'.$month.'/'.$day.' 00:00:01'));
            $dayStr = date('l', $time);
            if ($dayStr == 'Saturday' || $dayStr == 'Sunday') {
                $weekdays++;
            }
        }

        return $weekdays;
    }

答案 7 :(得分:0)

简单的天数代码:

function dayCount($day,$month,$year){

    $totaldays = date('t',strtotime($year.'-'.$month.'-01'));
    $countday = 4;
    if(($totaldays - $day) >= 28 ){
        $countday = 5;      
    }
    return  $countday;

}

echo dayCount(1,9,2019);