PHP Question:
_____________________________________________
Write the PHP code to list out all of the dates of the current month and assign them to their given days. As an example, for the month of October 2020, the output should look something like this:
October 2020
Monday: 5, 12, 19, 26
Tuesday: 6, 13, 20, 27
Wednesday: 7, 14, 21, 28
Thursday: 1, 8, 15, 22, 29
Friday: 2, 9, 16, 23, 30
Saturday: 3, 10, 17, 24, 31
Sunday: 4, 11, 18, 25
IMPORTANT: The code MUST use the current month,
whatever the current month is at the time (so if I used this PHP
code during July of 2020, it would use all of the dates of July
2020 and assign them to the proper days). Do NOT
use tables for this code.
Php Code
<?php
//create array for stored dates
$monday = array();
$tuesday = array();
$wednesday = array();
$thursday = array();
$friday = array();
$saturday = array();
$sunday = array();
$date = date("Y-m-d"); // get current date
$month = date("m"); // get current month
$year = date("Y"); // get current year
$last_date = date("t", strtotime($date)); //get last date in
current month
$m=$tu=$w=$th=$f=$sa=$su = 0; // assign same value to multiple
variable at once
//Print current month and year
if($month == "01")
{
echo "January ".$year."<br>";
}
elseif ($month == "02") {
echo "February ".$year."<br>";
}
elseif ($month == "03") {
echo "March ".$year."<br>";
}
elseif ($month == "04") {
echo "April ".$year."<br>";
}
elseif ($month == "05") {
echo "May ".$year."<br>";
}
elseif ($month == "06") {
echo "June ".$year."<br>";
}
elseif ($month == "07") {
echo "July ".$year."<br>";
}
elseif ($month == "08") {
echo "August ".$year."<br>";
}
elseif ($month == "09") {
echo "September ".$year."<br>";
}
elseif ($month == "10") {
echo "October ".$year."<br>";
}
elseif ($month == "11") {
echo "November ".$year."<br>";
}
elseif ($month == "12") {
echo "December ".$year."<br>";
}
for($i=1;$i<=$last_date;$i++) // for loop start first date to
last date in current month
{
$day = date("l", mktime(0,0,0,$month,$i,$year));//get
day of each date (change date using i variable)
//compare day if true store date in array
if($day == "Monday")
{
$monday[$m++] = $i;
}
elseif ($day == "Tuesday") {
$tuesday[$tu++] = $i;
}
elseif ($day == "Wednesday") {
$wednesday[$w++] = $i;
}
elseif ($day == "Thursday") {
$thursday[$th++] = $i;
}
elseif ($day == "Friday") {
$friday[$f++] = $i;
}
elseif ($day == "Saturday") {
$saturday[$sa++] = $i;
}
elseif ($day == "Sunday") {
$sunday[$su++] = $i;
}
}
$monDate=$tueDate=$wedDate=$thuDate=$friDate=$satDate=$sunDate=""; // assign same value to multiple variable at once
//start Monday
echo "Monday: ";
for($i=0;$i<sizeof($monday);$i++)
{
$monDate = $monDate.$monday[$i].", ";//concate string
of monday dates
}
echo substr(trim($monDate), 0, -1);//use substr and trim function
or remove the last comma in string
echo "<br>";
//end Monday
//start Tuesday
echo "Tuesday: ";
for($i=0;$i<sizeof($tuesday);$i++)
{
$tueDate = $tueDate.$tuesday[$i].", ";//concate string
of tuesday dates
}
echo substr(trim($tueDate), 0, -1);
echo "<br>";
//end Tuesday
//start Wednesday
echo "Wednesday: ";
for($i=0;$i<sizeof($wednesday);$i++)
{
$wedDate = $wedDate.$wednesday[$i].", ";//concate
string of wednesday dates
}
echo substr(trim($wedDate), 0, -1);
echo "<br>";
//end Wednesday
//start Thursday
echo "Thursday: ";
for($i=0;$i<sizeof($thursday);$i++)
{
$thuDate = $thuDate.$thursday[$i].", ";//concate
string of thursday dates
}
echo substr(trim($thuDate), 0, -1);
echo "<br>";
//end Thursday
//start Friday
echo "Friday: ";
for($i=0;$i<sizeof($friday);$i++)
{
$friDate = $friDate.$friday[$i].", ";//concate string
of friday dates
}
echo substr(trim($friDate), 0, -1);
echo "<br>";
//end Friday
//start Saturday
echo "Saturday: ";
for($i=0;$i<sizeof($saturday);$i++)
{
$satDate = $satDate.$saturday[$i].", ";//concate
string of saturday dates
}
echo substr(trim($satDate), 0, -1);
echo "<br>";
//end Saturday
//start Sunday
echo "Sunday: ";
for($i=0;$i<sizeof($sunday);$i++)
{
$sunDate = $sunDate.$sunday[$i].", ";//concate string
of sunday dates
}
echo substr(trim($sunDate), 0, -1);
?>
===============================OUTPUT============================
==Please Upvote==
Get Answers For Free
Most questions answered within 1 hours.