In an attempt to use selection lists in php ,i tried it for displaying date on a web page

<?php
echo"<html>
<head><title>Select a Date  </title></head>
<body>";
$monthname=array(1=>"January","Februray","March","April","May","June","July","August","September","October","November","December");
 $time=time();
 $today_date=date("M-d-Y",$time); //returns the current date
 echo "<div style='text-align:center'>\n";
 echo "<h3>Today is $today_date</h3><hr />\n";
 echo "<form action='process_form_date.php' method='POST'>\n";
 //selection list for month
 $todayMO=date("M",$time); //returns the current month
 echo "<select name='dateMonth'>\n";
 for ($M=1;$M<=12;$M++)
 {
  echo "<option value = $M";
  if($todayMO == $M)
   echo "selected";
   echo "> $monthname[$M]\n</option>"; 
 }
   echo "</select>\n";

 //selection list for day
 $todayDay=date("d",$time);
 echo "<select name='dateDay'>\n";
 for($n=1;$n<=31;$n++)
 {
   echo "<option value = $n";
   if($todayDay == $n)
     echo "selected";
 echo ">$n</option>\n";
 }
  echo "</select>\n";

 //selection list for year
 $todayYear=date("y",$time);
 echo "<select name='dateYear'>\n";
 for($n=$todayYear;$n<=$todayYear+3;$n++)
 {

   echo "<option value = $n";
   if($todayYear == $n)
     echo "selected";
 echo ">$n</option>\n";
 }
  echo "</select>\n";
  echo "</form></div>\n";
 ?>

</body>
</html>

But this code is not using the 'select' option to display today's date in a selection list but rather displaying for me
January-1-2014(default in code.)
How to get it?
a3c031e6a8b7a57aef51ceb9b783667e

Recommended Answers

All 5 Replies

You are missing a space in following places
Use

echo "<option value = $M ";

instead of

echo "<option value = $M";

Use

echo "<option value = $n ";

instead of

echo "<option value = $n";

Use

echo "<option value = $n ";

instead of

echo "<option value = $n";

You are missing a space in following places
Use

echo "<option value = $M ";

instead of

echo "<option value = $M";

Use

echo "<option value = $n ";

instead of

echo "<option value = $n";

Use

echo "<option value = $n ";

instead of

echo "<option value = $n";

You are quite rite but it still not working for Month(year,day done with it)

Yep.. You have to get the integer value of the month.

Use

$todayMO=date("m",$time);

instead of

$todayMO=date("M",$time);

Thank you so much for your instant replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.