Warning: fopen(graphic_design/files/thread-1010-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened PHP Date Select Box Functions -
PDA

View Full Version : PHP Date Select Box Functions


Arch Stanton
06-08-2004, 08:40 AM
Here are some functions I created today. They save you from having to make select boxes for dates. I normaly hate using select boxes because they can be a bit cumbersome, but when it comes to making sure you get valid data, nothing works better.

Month Select Box:

function monthSelect($fieldName,$selected) {
?>
<select name="<?=$fieldName?>">
<option value="">Choose Month</option>
<option value="01"<? if ($selected=="01") { echo " selected"; } ?>>January</option>
<option value="02"<? if ($selected=="02") { echo " selected"; } ?>>February</option>
<option value="03"<? if ($selected=="03") { echo " selected"; } ?>>March</option>
<option value="04"<? if ($selected=="04") { echo " selected"; } ?>>April</option>
<option value="05"<? if ($selected=="05") { echo " selected"; } ?>>May</option>
<option value="06"<? if ($selected=="06") { echo " selected"; } ?>>June</option>
<option value="07"<? if ($selected=="07") { echo " selected"; } ?>>July</option>
<option value="08"<? if ($selected=="08") { echo " selected"; } ?>>August</option>
<option value="09"<? if ($selected=="09") { echo " selected"; } ?>>September</option>
<option value="10"<? if ($selected=="10") { echo " selected"; } ?>>October</option>
<option value="11"<? if ($selected=="11") { echo " selected"; } ?>>November</option>
<option value="12"<? if ($selected=="12") { echo " selected"; } ?>>December</option>
</select>
<?
}

Day select box:

function daySelect($fieldName,$selected,$numDays) {
if (!$numDays) {
$numDays = 31;
}

?>
<select name="<?=$fieldName?>">
<option value="">Choose Date</option>
<?
for ($i = 1; $i <= $numDays; $i++) {
?>
<option<? if ($selected==$i) { echo " selected"; } ?>><?=$i?></option>
<?
}
?>
</select>
<?
}

Year select box:

function yearSelect($fieldName,$selected,$startYear,$numYears) {
if (!$startYear) {
$startYear = (date('Y')-1);
}
if (!$numYears) {
$numYears = 10;
}
?>
<select name="<?=$fieldName?>">
<option value="">Choose Year</option>
<?
for ($i = 0; $i < $numYears; $i++) {
$thisyear=$startYear+$i;
?>
<option<? if ($selected==$thisyear) { echo " selected"; } ?>><?=$thisyear?></option>
<?
}
?>
</select>
<?
}
No instructions on use at this time, I'm too busy, maybe later. For now, if you have any questions, just post em here.

Octane
06-08-2004, 09:56 AM
not to say that there is anything wrong with what you posted, but there is a better way to do the month ... and using the <?= is considered "bad coding", but it's definately code that gets repeated way to many times, so a couple functions are the way to go.


<?php
function monthSelect($fieldName,$selected=0)
{
echo <<< TEOL
<select name="$fieldName">
<option value="">Choose Month</option>
TEOL;

for ($i=1; $i < 13; $i++)
{
$tmpdate = mktime(0,0,0,$i);
$selectedoption = $selected == $i ? ' selected' : '';
echo strftime("<option $selectedoption value=\"%m\">%B</option>\n", $tmpdate);
}
echo "</select>";
}

function yearSelect($fieldName,$startYear,$selected=0,$numYea=10)
{
if (!$startYear) { $startYear = (date('Y')-1); }
if (!$numYears) { $numYears = 10; }

echo <<< TEOL
<select name="$fieldName">
<option value="">Choose Year</option>
TEOL;

for ($i = 0; $i < $numYears; $i++)
{
$thisyear=$startYear+$i;

echo "<option ".($selected==$thisyear ? "selected" : '').">$thisyear</option>\n";

}
echo "</select>\n";
}



function daySelect($fieldName,$selected=0,$numDays=31)
{
if (!$numDays) { $numDays = 31; }

echo <<< TEOL
<select name="$fieldName">
<option value="">Choose Date</option>
TEOL;

for ($i = 1; $i <= $numDays; $i++)
{
echo "<option ".($selected==$i ? ' selected' : '').">$i</option>\n";
}

echo "</select>";
}

?>


I used strftime() instead of date(), because I work with a lot of foreign countries, and if i change the locale, then I will get the appropriate months, as well, but not with date().

Arch Stanton
06-08-2004, 10:21 AM
why is using "<?=" considered bad coding?

I use it all of the time, breaking in and out of php seems to give my pages a more "html" feel to them (my editor switches from php syntax highlighting to html, then my html doesn't look like one monochrome string). Maybe it's bad, but it works for me. If there is a good reason not to do it, I'd like to know.

I like your use of the ternary operator. I gotta start using those more.

Koobi
06-08-2004, 12:38 PM
PEAR coding standards (http://pear.php.net/manual/en/standards.tags.php) is the suggested coding style but I hate it when I have to have the opening bracket of an IF condition or a function on the same line. I prefer C/C++ style indenting.

I'm not sure, but I think I once read somewhere that using <?= might become deprecated in future versions plus it's not very pleasant to read either as is the case with the ternary operator but I must admit, the ternary operator is great to cut down on lines and lines of code so I use it for those little parts of code that people really don't need to read:)

I prefer using heredoc instead of excaping to X/HTML. Just to add to what Octane said, you can also use heredocs like this:

$myString =<<< MYSTR
<p>I'm in a $mood mood.</p>
MYSTR;

if($userFeeling == 'good')
{
$mood = $userFeeling;
}
else
{
$mood = 'bad';
}

echo $myString;

Octane
06-08-2004, 10:21 PM
Bane is right ... there is word that the <?= is to be depricated ... also, it is an option that can be shut off on a server (called ASP-something-rather-option), and anything that can be disabled can run havoc when porting.

Yes, I agree the ternary can be hard to read, especially if you aren't used to seeing it, but it's nice because you can use it in between quoted strings to echo a condition, which can't be done otherwise unless done outside of the string. It's just something that Wade got me into and I feel like I'm smarter when I use it ;)

As for heredoc, i love it ... it makes echoing HTML so much nicer so that you don't have to escape single & double-quotes and don't need to take the extra step of going back into X/HTML mode (which, if done a lot in a page can slow down the loading).

And lastly, I agree with Bane ... I prefer not to use the {} for single-lined condition statements, but as I have been trying to work with the PEAR standards, I've slowly stopped using it ... but, i'm glad that it agrees with putting the { on the next line instead at the end of the condition statement ... it only makes sense that the open and closed brackets match up in a line.

Koobi
06-09-2004, 01:48 AM
Bane is right ... there is word that the <?= is to be depricated ... also, it is an option that can be shut off on a server (called ASP-something-rather-option), and anything that can be disabled can run havoc when porting.


Thanks for confirming that Octane :)
But I thought the ASP style tag was <% ?

Octane
06-09-2004, 09:24 AM
Ooops, my bad .. you are right. Wasn't thinking straight ... the <? and <?= are short_tags ... i was thinking they were ASP because the methodoligy is ASP style, as it doesn't start like <%asp. At any rate, I've still heard that <?= is to be depricated and both <? and <?= can be disabled from server to server, so shouldn't be used.