Pankaj Mahadik
Pankaj Mahadik

Reputation: 35

Count number of Sunday's in given Month and year

I want output to be count of sunday's present in given month and year.

This is my code:

$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++;
    }
}

I am not getting any output if i echo $num_sundays. Please help me . I am new to PHP

Upvotes: 3

Views: 8280

Answers (12)

Amit Yadav
Amit Yadav

Reputation: 11

We can use ($total_days - $first_sunday)/7 to reduce the number of iteration of for loop, like:

$total_days = cal_days_in_month(CAL_GREGORIAN,$month,$year);
for($fs = 1; $fs <= 7; $fs++){
    $day = date("l", mktime(0, 0, 0, $month,$fs));
    if($day == 'Sunday'){
        break;
    }
}
$remainingSundays = floor(($total_days - $fs)/7);
$totalSundays = $remainingSundays + 1;

Upvotes: 0

Cal Lidderdale
Cal Lidderdale

Reputation: 1

The does all 4 or 5 weeks of the month. Could be set to only start at the fourth.

function numdaysmonth ($dow, $month)
{
 $woma = array ("", "first", "second", "third", "fourth", "fifth");
 $count=0;
 $t=0;
 for ($n=1; $n<=5; $n++)
 {
        $str = sprintf ("%s %s of $month", $woma[$n], $dow);
        $u =  date('U', strtotime($str)) . "\n"; 
        $l = $t;
        $t = date('m', $u);
        if ($l == $t || $l == 0) $count++;
  #      if ($l == $t || $l == 0) echo date('Y/m/d', $u) . "\n";
 }
 return($count);
}

$mo = array ("January","February","March","April","May","June","July","August","September","October","November","December");
foreach ($mo as $m)
{
 $ret =  numdaysmonth ("Saturday", "$m");
 echo "RET $m $ret\n";
}

Upvotes: 0

Muhammad Hafizullah
Muhammad Hafizullah

Reputation: 11

Here is simple code to get number of Sunday in a month

<?php
$month = date("m");
$year = date("Y");
$d=cal_days_in_month(CAL_GREGORIAN,2,$year);
#echo date("l", mktime(0, 0, 0, $month,$d));
$total = 0;
for($i = 1; $i <= $d; $i++){
    $sunday = date("l", mktime(0, 0, 0, $month,$i));
    if($sunday == 'Sunday'){
        $total++;
    }
    
}
echo "There are total $total sundays in ".$month = date("F", mktime(0, 0, 0, $month));;


?>

Upvotes: 0

IMAL UOP
IMAL UOP

Reputation: 1

$day =Carbon\Carbon::now("Asia/Kolkata")->daysInMonth;
        echo $day.'</br>';
        if($day = 28) {
            $weekend = 8;
        }elseif($day = 29) {
            $first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
            $first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
            if($first_day_saturday || $first_day_sunday) {
                $weekend = 9;
            }else {
                $weekend = 8;
            }
        }elseif($day = 30) {
            $first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
            $first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
            if ($first_day_saturday) {
                $weekend = 10;
            }elseif ($first_day_sunday) {
                $weekend = 9;
            }else {
                $weekend = 8;
            }
        }else {
            $first_day_thrusday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isThursday();
            $first_day_friday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isFriday();
            $first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
            $first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
            if ($first_day_friday || $first_day_saturday) {
                $weekend = 10;
            }elseif ($first_day_sunday || $first_day_thrusday) {
                $weekend = 9;
            }else {
                $weekend = 8;
            }
        }
        echo $weekend.'</br>';

Upvotes: 0

Gopal Karmakar
Gopal Karmakar

Reputation: 1

Simple code for day count:

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);

Upvotes: 0

Priya Gohil
Priya Gohil

Reputation: 1

Use this code if it helps you

   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;
    }

Upvotes: 0

Al Emran
Al Emran

Reputation: 76

Check following Example. I work perfectly..

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);

Upvotes: 1

nggit
nggit

Reputation: 720

Without loop. I hope this gives the correct results.

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

Upvotes: 2

Sadee
Sadee

Reputation: 3180

To get a count of any given day in a given month in a year:

    $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";

Upvotes: 0

vikas pandey
vikas pandey

Reputation: 204

working example 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>";
}

Upvotes: 0

devpro
devpro

Reputation: 16117

You just need to remove <br> from these two lines:

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

Otherwise this will be the part of start and end date, and your strtotime() will return false.

Example:

<?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: This will return false for both.

Example 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: This will return the values

Complete Example:

<?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: This will return 4 sunday

Upvotes: 5

pawan sen
pawan sen

Reputation: 726

Get all sunday in month see below code:

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

Upvotes: 1

Related Questions