Link to home
Start Free TrialLog in
Avatar of maeve100
maeve100

asked on

List of Fridays

Hi there,

I hack and slashed this code together a long time ago - I didn't understand it 100% then, and I still don't understand it 100% now.  All I know is that it will make a list of all the current Fridays in a year.  Can you help me understand & comment (so I will remember) whats going on in these loops to grab all the Friday dates?  How do I grab all the Sundays instead?  What is the "list($n,$d)..." line all about?  

Thank you!

function fridays($month, $year, $newsletter_list)
{ 
  // $newsletter_list is an array of exsisting newsletter dates from the database.  IE. 2012-11-04, 2012-11-25..
  $days = array();
  
  for ($month = 1; $month <= 12; $month++) {
	  list($n,$d) = explode('-', date('t-d', strtotime("Friday, $year-$month")));
	  while ($d <= $n)
	  {
		$selectedDay = sprintf("%04d-%02d-%02d", $year, $month, $d);
		$ontheList = false;
		
		for ($q = 0; $q < count($newsletter_list); $q++) {
			if ($selectedDay == $newsletter_list[$q]) {
				$ontheList = true;
			}																		
		}
		
		if ($ontheList == false) {
			$days[] = $selectedDay;
		}
		
		$d += 7;
	  }
  }
  return $days;
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Also, please see this article.  It has useful learning resources for those who are new to PHP and want to learn more.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some other anomolies I noticed

You are passing $month to the function - but you never use this value because you immediately overwrite it in the for loop.
list($n,$d) = explode('-', date('t-d', strtotime("Friday, $year-$month")));

Open in new window

Any time you have code that looks like this, you have something that can lead to confusion.  The accumulation of closing parentheses is indicative of multiple function calls in a single line of code.  And in beginner programming that is almost always indicative of code that was copied from an external reference, usually without a good understanding of what the code was intended to do.

To deconstruct that line, first go to the innermost function call, which is strtotime(), and work your way out from there.  Its output is expected to be a unix timestamp (but could also be FALSE, which PHP will evaluate as if it were zero).  This value will be part of the arguments passed to date().  

The output of date() will be determined by the pattern 't-d' which will give you the number of days in the given month, followed by a dash, followed by the day of the month with a leading zero, if needed, to pad the day to 2 digits.  This will be returned in the form of a string, either 4 or 5 characters.  

This string will be passed to explode(), which will use the literal string dash '-' as a delimiter to create an array.  The zero position of the array will be the output from date('t') and the one position of the array will be the output from date('d').  

This array will be assigned to the PHP variables $n and $d by the list() statement.

That's a lot of moving parts for a single line of code.  For some reason, inexperienced programmers look at stuff like this and think, "cool."  More experienced programmers tend to avoid that kind of "cool" choosing instead to write programming that easy to read and debug, well-documented with comments, nicely formatted control structures, etc.  The computer doesn't care what you write as long as it functions properly.  But the people who read the code care, and they need to be able to understand your intent at a glance.  Those people include you, if you put the programming aside and come back to it a day, or even a few hours, later.

I have had enough bad experiences looking at incomprehensible code that I have come to believe that my own code should be easy to read and understand.  So while I tolerate a few compound statements like stacking date() and strtotime(), I generally require my code to do only one thing per line.  It's easier to test and it's easier to keep my thought processes in order when the code is laid out that way.