How to resolve the algorithm Last Friday of each month step by step in the PHP programming language
How to resolve the algorithm Last Friday of each month step by step in the PHP programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the PHP programming language
1. last_friday_of_month
Function:
- This function calculates the last Friday of a given month and year.
- It iterates through the range of days in the month, starting from the last day.
- For each day, it checks if it falls on a Friday (day of week = 5).
- If a Friday is found, it returns the date in the format "YYYY-MM-DD".
2. print_last_fridays_of_month
Function:
- This function prints the last Fridays of all months in a given year.
- It iterates through the months (1 to 12) and calls the
last_friday_of_month
function for each month. - The dates are printed in order from January to December.
3. Main Code:
- Sets the default timezone to GMT.
- Calls the
print_last_fridays_of_month
function for the year 2012.
Example Output:
For the year 2012, the output will be:
2012-01-27
2012-02-24
2012-03-30
2012-04-27
2012-05-25
2012-06-29
2012-07-27
2012-08-31
2012-09-28
2012-10-26
2012-11-30
2012-12-28
How it Works:
The function last_friday_of_month
uses the mktime
function to generate a timestamp for the last day of the month. It then uses the date
function to check the day of week for that timestamp. If it's not a Friday, it decrements the day by 1 and repeats the process until a Friday is found.
The print_last_fridays_of_month
function iterates through all months of the year and calls the last_friday_of_month
function for each month. The dates are then printed in order.
Source code in the php programming language
<?php
function last_friday_of_month($year, $month) {
$day = 0;
while(True) {
$last_day = mktime(0, 0, 0, $month+1, $day, $year);
if (date("w", $last_day) == 5) {
return date("Y-m-d", $last_day);
}
$day -= 1;
}
}
function print_last_fridays_of_month($year) {
foreach(range(1, 12) as $month) {
echo last_friday_of_month($year, $month), "<br>";
}
}
date_default_timezone_set("GMT");
$year = 2012;
print_last_fridays_of_month($year);
?>
You may also check:How to resolve the algorithm Hello world/Text step by step in the Prolog programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Scala programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Vampire number step by step in the Clojure programming language