/    Sign up×
Community /Pin to ProfileBookmark

Drop down input for dates

Hi All,

I want to make a drop down box (rather 3) for date. month and year. I heard from the forums that there is no ready made function available to do that. After searching, I got a script from the phpclasses forum which goes like this:

#####

<?php
class DateDDLGenerator{

var $intYear;
var $intMonth;
var $intDay;
var $bolSetToCurrentDay;

function DateDDLGenerator(){
$this->bolSetToday = false;
$this->intYear = date(“Y”);
$this->intMonth = date(“m”);
$this->intDay = date(“d”);
}

function setToCurrentDay(){
$this->bolSetToCurrentDay = true;
}

#Generate Year range
function genYearDDL($selName = ‘Year’, $yearCount = 50, $year = ”){
/*
Check if the year passed in is the same as current year.
If the year got is not given or same as current year, the list
will select the current year by default. Otherwise, $yearSelect
will be set to what user entered.
*/
$yearSelect = $year == ” ? date(“Y”) : $year;

/*
$yearCount: it is the length of your drop down list, i.e. how many
years do you want to show. It is 50 by default, which shows 50 years
from now.
*/

$str = “<select name=’$selName’>n”;
for($i = $yearSelect; $i >= ($yearSelect – $yearCount); $i–){
if($this->bolSetToCurrentDay == true){
$selected = $this->intYear == $i ? ‘selected=”selected”‘ : ”;
}
$str .= “t<option value=’$i’ $selected>$i</option>n”;
}
$str .= “</select>n”;
print $str;
}

#Generate month range from 1 to 12
function genMonthDDL($selName = ‘Month’, $date_format = ‘short’){
$shortM = array(1 => “Jan”, “Feb”, “Mar”,
“Apr”, “May”, “Jun”,
“Jul”, “Aug”, “Sep”,
“Oct”, “Nov”, “Dec”);

$longM = array(1 => “January”, “February”, “March”,
“April” , “May” , “June” ,
“July” , “Aug” , “September”,
“October”, “November”, “December”);

$str = “<select name=’$selName’>n”;
if($date_format == ‘short’){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? ‘selected=”selected”‘ : ”;
}
$str .= “t<option value=’$i’ $selected>”.$shortM[$i].”</option>n”;
}
}elseif($date_format == ‘long’){
for($i = 1; $i <= 12; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intMonth == $i ? ‘selected=”selected”‘ : ”;
}
$str .= “t<option value=’$i’ $selected>”.$longM[$i].”</option>n”;
}
}
$str .= “</select>n”;

print $str;
}

#Generate day range from 1 to max days of relevant month
function genDayDDL($selName = ‘Day’){
$str = “<select name=’$selName’>n”;

//Thanks to Peter K on this improvement and now this method support leap year
if ($this->intMonth == 2) { // February ?
$leap_day = 0;

if ($this->intYear >= 4 && $this->intYear % 4 == 0) { // Leap year ?
if ($this->intYear >= 1800 && $this->intYear % 100 == 0) { // No accurate leap centuries before that
if (($this->intYear / 100) % 4 == 0)
$leap_day = 1;
} else
$leap_day = 1;
}

$max_days = 28 + $leap_day;
} else if ($this->intMonth == 4 || $this->intMonth == 6 ||
$this->intMonth == 9 || $this->intMonth == 11)
$max_days = 30;
else
$max_days = 31;

for($i = 1; $i <= $max_days; $i++){
if($this->bolSetToCurrentDay == true){
$selected = $this->intDay == $i ? ‘selected=”selected”‘ : ”;
}
$str .= “t<option value=’$i’ $selected>$i</option>n”;
}
$str .= “</select>n”;
print $str;
}

}
?>

#####

They call it a class file and I don’t know how to use a class file in php. Can anyone help me please?

Thanks in advance.

With love, Anirban.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 17.2009 — I'm not going to try to teach you object-oriented programming in a forum reply, but I'll point you to a tutorial or two:

PHP 101 (Part 7)

Getting started with objects with PHP v5
Copy linkTweet thisAlerts:
@mackilMar 18.2009 — I want to make a drop down box (rather 3) for date. month and year.[/QUOTE]

A far quicker way would be to use the PHP date functions (http://us3.php.net/date).

For Example:
[code=php]
Month: <select name="month" size="1">
<?php
for($x = 1; $x < 13; ++$x){?>
<option value="<?=$x?>"><?=date("F", mktime(0, 0, 0, $x, 1, date("Y")))?></option>
<?php
}?>
</select>

Year: <select name="year" size="1">
<?php
for($x = 0; $x < 8; ++$x){?>
<option value="<?=date("Y")+$x?>"><?=date("Y")+$x?></option>
<?php
}?>
</select>
[/code]
×

Success!

Help @anix_anirban spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...