?
Solved

PHP Date Object, Find last friday in a month

Posted on 2008-06-10
6
Medium Priority
?
1,217 Views
Last Modified: 2012-08-13
Hi Guys

I need to find out what date the last friday in a month is. Is there a way of doing this with php?

Thanks
0
Comment
Question by:tim_carter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Author Comment

by:tim_carter
ID: 21750808
I found out a way my self. Thanks

$NumberOfDaysInCurrentMonth=cal_days_in_month(CAL_GREGORIAN,$Month,$Year);
      
      $LastFridayIs = 0;
      for ($i=1;$i<$NumberOfDaysInCurrentMonth;$i++){
            $DayNameTemp = date("D", mktime(0, 0, 0, $Month, $i, $Year));
            if($DayNameTemp=='Fri')
                  $LastFridayIs = $i;
      }
      echo "LastFridayIs=".$LastFridayIs."<br>";
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 21760401
Hey, Tim.  Even tho you already solved it, I thought it was a good exercise so I did it this way. I have a friend who hosts a party on the fifth Friday.  Now I can mark my calendar in advance! ~Ray
<?php // RAY_lastfriday.php -- Find Last Friday in a Month
/* ********************************************************************** */
function last_friday($date) {
 
// GET A VALID TIMESTAMP IN CASE THE GIVEN DATE IS BOLLIXED
	if (!$now = strtotime($date)) { $now = strtotime('today'); }
 
// GET SECONDS IN A DAY FOR TIMESTAMP CALCULATIONS
	$one_day	= 60*60*24;
 
// GET THE NUMBER OF THE CURRENT MONTH
	$my_month	= date("n", $now);
 
// GET THE TIMESTAMP OF THE LAST DAY OF THE CURRENT MONTH
	$last_day	= mktime(0,0,0, date("m", $now)+1, 0, date("Y", $now));
 
	$fridays	= array();
	while (2 > 1) { // LOOP ENDS ON BREAK WHEN MONTH CHANGES
		$last_date	= date("D", $last_day);
		if ($last_date == 'Fri') {
			$last_month	= date('n', $last_day);
			if ($last_month != $my_month) {
				break;
			} else {
				$fridays[] = date('Y-m-d D', $last_day);
			}
		}
		$last_day	= $last_day - $one_day;
	}
return $fridays;
}
/* ********************************************************************** */
 
echo "<pre>\n";
 
/* USAGE */
 
/* FIND THE LAST FRIDAY IN THE CURRENT MONTH */
$my_fridays = last_friday('now');
print_r($my_fridays[0]);
 
echo "\n\n";
 
/* FIND ALL THE FRIDAYS IN ANOTHER MONTH */
/* NOTE THE ARRAY IS IN REVERSE CHRONOLOGICAL ORDER - YOU MIGHT WANT array_reverse(); */
$my_fridays = last_friday ('February 4, 2008');
print_r($my_fridays);
 
echo "\n\n";
 
/* FIND THE MONTHS IN THIS YEAR WITH FIVE FRIDAYS AND PRINT THE DATE OF THE FIFTH FRIDAY */
$month = 1;
while ($month < 13) {
	$my_fridays = last_friday(date('Y') .'-'. $month . '-1');
	if (count($my_fridays) == 5) {
		echo "$my_fridays[0] \n";
	}
	$month	= $month + 1;
}

Open in new window

0
 
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 1000 total points
ID: 21761199
I think I have a neater one, no loops or string matching required:
<?php
function lastfriday($date) {
	//Find first day of following month
	$nextmonth = strtotime(date('Y-m-01', strtotime("$date +1 month")));
	//Now find the friday before that
	$lastfriday = date('Y-m-d', strtotime('last Friday', $nextmonth));
	return $lastfriday;
}
 
echo lastfriday('2008-06-07')."\n";
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 21762097
@Squinky: Very cool!  strtotime() is sheer genius! ~Ray
0
 
LVL 1

Author Closing Comment

by:tim_carter
ID: 31465715
Thanks guys. Awesome ;) I just found out about the strtotime yesterday.. If only knew about that earlier, it could have saved me so much thinking ;)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 21781410
Right, strtotime() always beats thinking for me!  

You might enjoy some of the books from SitePoint:
http://www.sitepoint.com/books/library/

They are usually pretty good.  Happy coding!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question