4

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?

user1642439
  • 89
  • 1
  • 6

8 Answers8

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

if (date('m') === date('m', $fifth)) {
  $fridays[4] = date('Y-m-d', $fifth);
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
1

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?

Community
  • 1
  • 1
automaticAllDramatic
  • 2,025
  • 1
  • 21
  • 25
1

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;
}
Abbas
  • 552
  • 5
  • 8
0

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...

Community
  • 1
  • 1
Chris
  • 5,516
  • 1
  • 26
  • 30
  • but fifth friday is outputting me 2012-10-05, means next month – user1642439 Sep 24 '12 at 08:52
  • Take a look at the link - it will do exactly what you are wanting. I didn't quite realise until I had already posted, hence the amount of edits haha! – Chris Sep 24 '12 at 08:54
0

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

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

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.

0
<?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";
}

?>
  • 1
    Hey and welcome to Stack Overflow! If you are looking to contribute, you may try sorting your preferred tags by new questions, as this question is quite old - nearly 8 years. Additionally, it is helpful if you provide some information along with your code to help others understand. And a little more directly, you may want to update your PHP version, as 7.0.* has been out of even extended support for more than a year now (see: [lifecycle, supported versions](https://www.php.net/supported-versions.php)). – zbee Feb 07 '20 at 19:05