jeeva Posted August 4, 2008 Share Posted August 4, 2008 Hi Frnds, I want to get how many fridays is there in a particular month? I have spend an hour on searching in net but i couldn't able to get this... anyone has done this? Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/ Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 Yeah I had some spare time so I wrote the script for you. I tested a few months, worked... So hopefully it's all good: <?php //The month and year $month = "May"; $year = 2008; //Get the number of days for the selected month... $numberOfDaysInMonth = date("t", strtotime($month)); //First find out the first day of the month... 0 (sunday) and so on... $firstDayOfMonth = date("w", strtotime("1 {$month} {$year}")); //Set numberOfFridays to a default of 0 $numberOfFridays = 0; //Now we need to loop through every day of the month and check if it is a friday for($i = 1, $s = $firstDayOfMonth; $i <= $numberOfDaysInMonth; $i++){ if($s == 5){ //5 is friday, so when s == 5 it is a friday. So we add 1 to the friday tally. $numberOfFridays += 1; } if($s == 6){ //We have reached Saturday, start again... $s = 0; }else{ $s += 1; //Add one to the day... } } echo "In {$month} of {$year} there are {$numberOfFridays} Fridays!"; ?> Hope it helps! Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-607405 Share on other sites More sharing options...
MadTechie Posted August 4, 2008 Share Posted August 4, 2008 LOOL, also had some spare time.. theirs probably a better why but try this <?php $year = 2008; $month = 1; //Aug $day = 1; $startDate = mktime(0, 0, 0, $month, $day, $year); $NoD = date( "t", $startDate); While(date( "D", $startDate) != "Fri") //find first Friday { $day++; $startDate = mktime(0, 0, 0, $month, $day, $year); } $day--; $days = ceil(($NoD-$day)/7); echo "$days Fridays"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-607411 Share on other sites More sharing options...
jeeva Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks frnds, I got this simple code .... <?php $month = 5; $year = 2008; $sd = mktime(0,0,0,$month,1,$year); $n = date('N',$sd); $fr = 5; //day of the week $frd = $fr-$n; $nof = 4; //default no.of fridays in a month $ad1 = mktime(0,0,0,$month,$frd+1+28,$year); if(date('m',$ad1)==$month) $nof++; echo $nof." fridays for the month ".date('M-Y',$sd); ?> Thanks Jeeva Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-607435 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 I tried your code: 5 fridays for the month Mar-2008 There is only 4 in March... Same with November 08, says there is 5... But there is 4. Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-607439 Share on other sites More sharing options...
jeeva Posted August 5, 2008 Author Share Posted August 5, 2008 yes, You r right...... Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-608356 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 Try mine or MadTechie's. I'm going to keep mine in my scripts library, may come in handy one day. Quote Link to comment https://forums.phpfreaks.com/topic/118071-day-count/#findComment-608372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.