Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP show today's date/time in form drop down lists
    primarykey
    data
    text
    <p>i have this long code to show today's date &amp; time in a form drop down lists. It does work fine but I was wondering if there's a simpler way to do it...?</p> <p>I want to show all months, days, years (only a couple of years), all hours (24 hour format), all minutes and all seconds but have the current date/time selected in each drop down list.</p> <pre><code>&lt;select name="event_month"&gt; &lt;? $arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December"); $date_m = date(m); $count = 1; $months = 12; do{ $month = $arr_m[$count-1]; if ($date_m == $count) { echo ("&lt;option value='$month' selected='selected'&gt;$month&lt;/option&gt;"); } else { echo ("&lt;option value='$month'&gt;$month&lt;/option&gt;"); } $count++; }while($count&lt;=$months); ?&gt;&lt;/select&gt; &lt;select name="start-day"&gt; &lt;? $date_d = date(d); $day=1; $days = 31; do{ if($date_d == $day){ echo ("&lt;option value='$day' selected='selected'&gt;$day&lt;/option&gt;"); }else{ echo ("&lt;option value='$day'&gt;$day&lt;/option&gt;"); } $day++; }while($day&lt;=$days); ?&gt;&lt;/select&gt; &lt;select name="start-year"&gt;&lt;? $date_y = date(Y); $year =intval($date_y); $years = $year+2; do{ if($date_y == $year){ echo ("&lt;option value='$year' selected='selected'&gt;$year&lt;/option&gt;"); }else{ echo ("&lt;option value='$year'&gt;$year&lt;/option&gt;"); } $year++; }while($year&lt;=$years); ?&gt;&lt;/select&gt; &lt;select name="start-hour"&gt;&lt;? $date_h = date(H); $hour = 1; $hours = 24; do{ if($date_h == $hour){ echo ("&lt;option value='$hour' selected='selected'&gt;$hour&lt;/option&gt;"); }else{ echo ("&lt;option value='$hour'&gt;$hour&lt;/option&gt;"); } $hour++; }while($hour&lt;=$hours); ?&gt;&lt;/select&gt; &lt;select name="start-min"&gt;&lt;? $date_min = date(i); $min = 1; $mins = 60; do{ if($date_min == $min){ echo ("&lt;option value='$min' selected='selected'&gt;$min&lt;/option&gt;"); }else{ echo ("&lt;option value='$min'&gt;$min&lt;/option&gt;"); } $min++; }while($min&lt;=$mins); ?&gt;&lt;/select&gt; &lt;select name="start-sec"&gt;&lt;? $date_sec = date(s); $sec = 1; $secs = 60; do{ if($date_sec == $sec){ echo ("&lt;option value='$sec' selected='selected'&gt;$sec&lt;/option&gt;"); }else{ echo ("&lt;option value='$sec'&gt;$sec&lt;/option&gt;"); } $sec++; }while($sec&lt;=$secs); ?&gt;&lt;/select&gt; </code></pre> <p><strong>-- UPDATE --</strong> here's the cleaner version - thanks!</p> <pre><code>&lt;select name="start-month"&gt; &lt;? $arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December"); $month = date('m'); for ($i = 0; $i &lt;= 12; $i++) { $name = $arr_m[$i-1]; $sel = ($i == $month) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$name&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;select name="start-day"&gt; &lt;? $day = date('d'); for ($i = 0; $i &lt;= 31; $i++) { $sel = ($i == $day) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$i&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;select name="start-year"&gt; &lt;? $year = date('Y'); $years = $year+2; for ($i = $year; $i &lt;= $years; $i++) { $sel = ($i == $year) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$i&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;select name="start-hour"&gt; &lt;? $hour = date('G'); for ($i = 0; $i &lt;= 24; $i++) { $sel = ($i == $hour) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$i&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;select name="start-min"&gt; &lt;? $min = date('i'); for ($i = 0; $i &lt;= 59; $i++) { $sel = ($i == $min) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$i&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;select name="start-sec"&gt; &lt;? $sec = date('s'); for ($i = 0; $i &lt;= 59; $i++) { $sel = ($i == $sec) ? ' selected="selected"' : ''; echo "&lt;option value=\"$i\"$sel&gt;$i&lt;/option&gt;"; } ?&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload