Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the First or Last Friday in a Month

Tags:

php

datetime

I'm trying to write a calendar function like this

function get_date($month, $year, $week, $day, $direction)
{
    ....
}

$week is a an integer (1, 2, 3...), $day is a day (Sun, Mon, ...) or number, whichever is easier. The direction is a little confusing, because it does a different calculation.

For an example, let's call

get_date(5, 2009, 1, 'Sun', 'forward');

It uses the default, and gets the first Sunday in May ie 2009-05-03. If we call

get_date(5, 2009, 2, 'Sun', 'backward');

, it returns the second last Sunday in May ie 2009-05-24.

like image 535
Zahymaka Avatar asked May 29 '09 03:05

Zahymaka


People also ask

How do you find the first Friday of the month?

Subtract that number from the day you are looking for; for example, if the first day of the month is Wednesday (2) and you're looking for Friday (4), subtract 2 from 4, leaving 2. If the answer is negative, add 7. Finally add that to the first of the month; for my example, the first Friday would be the 3rd.

How do I get the last Friday of the month in Excel?

Hi I know that =DATE(YEAR(A29),MONTH(A29)+1,0)-MOD(WEEKDAY(DATE(YEAR(A29),MONTH(A29)+1,0),2)+2,7) will give me the last Friday of the calendar month of the date in cell A29.

How do I get the first Friday of the month in Excel?

=CEILING(EOMONTH(A2,-1)-5,7)+6 Then the date is displayed in cell C2, it means that the first Friday of January 2015 is the date 1/2/2015.


2 Answers

The language-agnostic version:

To get the first particular day of the month, start with the first day of the month: yyyy-mm-01. Use whatever function is available to give a number corresponding to the day of the week. Subtract that number from the day you are looking for; for example, if the first day of the month is Wednesday (2) and you're looking for Friday (4), subtract 2 from 4, leaving 2. If the answer is negative, add 7. Finally add that to the first of the month; for my example, the first Friday would be the 3rd.

To get the last Friday of the month, find the first Friday of the next month and subtract 7 days.

like image 118
Mark Ransom Avatar answered Oct 06 '22 16:10

Mark Ransom


Perhaps it can be made quicker...
This was VERY interesting to code.

Please note that $direction is 1 for forward and -1 for backward to ease things up :)
Also, $day begins with a value of 1 for Monday and ends at 7 for Sunday.

function get_date($month, $year, $week, $day, $direction) {
  if($direction > 0)
    $startday = 1;
  else
    $startday = date('t', mktime(0, 0, 0, $month, 1, $year));

  $start = mktime(0, 0, 0, $month, $startday, $year);
  $weekday = date('N', $start);

  if($direction * $day >= $direction * $weekday)
    $offset = -$direction * 7;
  else
    $offset = 0;

  $offset += $direction * ($week * 7) + ($day - $weekday);
  return mktime(0, 0, 0, $month, $startday + $offset, $year);
}

I've tested it with a few examples and seems to work always, be sure to double-check it though ;)

like image 21
MartinodF Avatar answered Oct 06 '22 15:10

MartinodF