PHP Classes

File: DateDDLGenerator.class.php

Recommend this page to a friend!
  Classes of Ellery Leung   Date Drop Down List Generator   DateDDLGenerator.class.php   Download  
File: DateDDLGenerator.class.php
Role: Class source
Content type: text/plain
Description: Date Drop Down List generator main file
Class: Date Drop Down List Generator
Generate HTML form select inputs to pick dates
Author: By
Last change: Include Peter K improvement on genDayDDL() method.
Date: 17 years ago
Size: 3,382 bytes
 

Contents

Class file image Download
<?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;
    }
}
?>