PHP Exercises: Reads a date and prints the day of the date
PHP: Exercise-59 with Solution
Write a PHP program to that reads a date (from 2004/1/1 to 2004/12/31) and prints the day of the date. Jan. 1, 2004, is Friday. Note that 2004 is a leap year.
Two integers m and d separated by a single space in a line, m ,d represent the month and the day.
Input:Input numbers (real numbers) are separated by a space.
Input 0 to exit.
Pictorial Presentation:
Sample Solution:
PHP Code:
<?php
// Define an array of days in a non-leap year
$days = explode(',', '0,31,60,91,121,152,182,213,244,274,305,335');
// Define an array of corresponding day names
$format = explode(',', 'Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday');
// Continue reading lines from standard input until an empty line is encountered
while (($line = trim(fgets(STDIN))) !== '') {
// Extract month and day values from the input line
sscanf($line, '%d %d', $m, $d);
// Check if the month is 0, which indicates the end of input
if ($m === 0) {
break;
}
// Calculate the day of the year using the pre-defined array
$n = $days[$m - 1] + $d;
// Determine the day of the week using the pre-defined array of day names
$today = $format[$n % 7];
// Output the result
echo "The day is: " . $today, PHP_EOL;
}
?>
Sample Input:
9 15
Sample Output:
The day is: Thursday
Flowchart:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP program to test the specified Circumference of C1 and C2 intersect.
Next: Write a PHP program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php-exercises/php-basic-exercise-59.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics