user1642439
user1642439

Reputation: 89

How to get all fridays from current month

I have a problem,

$fridays = array();
$fridays[0] = date('Y-m-d', strtotime('first friday of this month'));
$fridays[1] = date('Y-m-d', strtotime('second friday of this month'));
$fridays[2] = date('Y-m-d', strtotime('third friday of this month'));
$fridays[3] = date('Y-m-d', strtotime('fourth friday of this month'));
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));

but there is no fifth friday. Some months have fifth fridays. How to check and not set the last item array?

Upvotes: 4

Views: 4508

Answers (8)

Salman Rahman Auvi
Salman Rahman Auvi

Reputation: 1

<?php //php 7.0.8

$offDays = array();

$date = date('2020-02');
$day= 'Friday';
$offDays[0] = date('d',strtotime("first {$day} of ".$date));
$offDays[1] = $offDays[0] + 7;
$offDays[2] =  $offDays[0] + 14;
$offDays[3] =  $offDays[0] + 21;
$offDays['last'] = date('d',strtotime("last {$day} of ".$date));

if($offDays[3] == $offDays['last']){
  unset($offDays['last']);
}
else {
  $offDays[4] = $offDays['last'];
  unset($offDays['last']);
}

foreach($offDays as $off){
    echo date('Y-m-d-D',strtotime(date($date."-".$off)));
    echo "\n";
}

?>

Upvotes: 0

Abbas
Abbas

Reputation: 560

I have a function to count fridays in a month to my very own application.... it could be helpful for someone.

function countFridays($month,$year){
	$ts=strtotime('first friday of '.$year.'-'.$month.'-01');
	$ls=strtotime('last day of '.$year.'-'.$month.'-01');
	$fridays=array(date('Y-m-d', $ts));
	while(($ts=strtotime('+1 week', $ts))<=$ls){
		$fridays[]=date('Y-m-d', $ts);
	}return $fridays;
}

Upvotes: 1

copyandpaster
copyandpaster

Reputation: 7

I used this as the basis for my own solution:

$fridays = array();
$fridays[0] = date('d',strtotime('first fri of this month'));
$fridays[1] = $fridays[0] + 7;
$fridays[2] =  $fridays[0] + 14;
$fridays[3] =  $fridays[0] + 21;
$fridays['last'] = date('d',strtotime('last fri of this month'));

if($fridays[3] == $fridays['last']){
  unset($fridays['last']);
}
else {
  $fridays[4] = $fridays['last'];
  unset($fridays['last']);
}

print_r($fridays);

I needed to get an array of every Friday in a month, even if there were 5 and this seemed to do the trick using the original question as my basis.

Upvotes: 0

Akshat Sharma
Akshat Sharma

Reputation: 31

for($i=0;$i<=5;$i++)
{
       echo date("d/m/y", strtotime('+'.$i.' week friday september 2012'));
}

Upvotes: 0

automaticAllDramatic
automaticAllDramatic

Reputation: 2063

You can do this using the PHP date function. Get the month you want in $timestamp and then do something like this:

<?php
function fridays_get($month, $stop_if_today = true) {

$timestamp_now = time();

for($a = 1; $a < 32; $a++) {

    $day = strlen($a) == 1 ? "0".$a : $a;
    $timestamp = strtotime($month . "-$day");
    $day_code = date("w", $timestamp);
    if($timestamp > $timestamp_now)
        break;
    if($day_code == 5)
        @$fridays++;

}

return $fridays;
}

echo fridays_get('2011-02');

You can find a similar post about this: In PHP, how to know how many mondays have passed in this month uptil today?

Upvotes: 1

xdazz
xdazz

Reputation: 160853

$fifth = strtotime('fifth friday of this month');

if (date('m') === date('m', $fifth)) {
  $fridays[4] = date('Y-m-d', $fifth);
}

Upvotes: 8

haynar
haynar

Reputation: 6030

the month will have 5 fridays only if it has 30 days and first friday is 1st or 2nd day of the month or if it has 31 days and the first friday is 1st, 2nd or 3rd day of the month, so you can make conditional statement based on this calculation for 5th friday

Upvotes: 0

Chris
Chris

Reputation: 5605

I'm not on a machine that I could test this but what about something along the lines of....

if(date('Y-m-d', strtotime('fifth friday of this month')) > ""){
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));
}

The link below does exactly the same thing and will cover exactly what you are wanting todo without clunky if statements like above..

VERY Similar reading: read more...

Upvotes: 0

Related Questions