Results 1 to 5 of 5

Thread: date drop down box

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default date drop down box

    Hi i am trying to get this code to make the day and the year a drop down box just like the month

    PHP Code:
    <?php
    // no direct access
    defined('_JEXEC') or die('Restricted access');

    class 
    CFieldsDate
    {
        
    /**
         * Method to format the specified value for text type
         **/         
        
    function getFieldData$value )
        {
            if( empty( 
    $value ) || !is_numeric($value))
                return 
    $value;
        
            return 
    gmstrftime'%d/%m/%Y' $value );
        }
        
        function 
    getFieldHTML$field $required )
        {
            
    $html    '';
            
            
    $day    = !empty($field->value) ? gmstrftime'%d' $field->value ) : '';
            
    $month    = !empty($field->value) ? JString::ltrim(gmstrftime'%m' $field->value ) , 0) : '';
            
    $year    = !empty($field->value) ? gmstrftime'%Y' $field->value ) : '';
            
    //         $myDate = new JDate($field->value);
    //         
    //         $day    = !empty($field->value) ? $myDate->toFormat( '%d' ) : '';
    //         $month    = !empty($field->value) ? JString::ltrim($myDate->toFormat( '%m' ) , 0) : '';
    //         $year    = !empty($field->value) ? $myDate->toFormat( '%Y' ) : '';        
                    
            
    $months    = Array(
                            
    JText::_('January'),
                            
    JText::_('February'),
                            
    JText::_('March'),
                            
    JText::_('April'),
                            
    JText::_('May'),
                            
    JText::_('June'),
                            
    JText::_('July'),
                            
    JText::_('August'),
                            
    JText::_('September'),
                            
    JText::_('October'),
                            
    JText::_('November'),
                            
    JText::_('December')
                            );

            
    $class    = ($field->required == 1) ? ' required' '';
            
    $html .= '<div class="hasTip" title="' $field->name '::' $field->tips '">';
            
    $html .= '<input type="textbox" size="3" maxlength="2" name="field' $field->id '[]" value="' $day '" class="inputbox validate-custom-date' $class '" /> ' JText::_('DD');
            
    $html .= '&nbsp;/&nbsp;<select name="field' $field->id '[]" class="select validate-custom-date' $class '">';

            for( 
    $i 0$i count($months); $i++)
            {
                if((
    $i 1)== $month)
                {
                    
    $html .= '<option value="' . ($i 1) . '" selected="true">' $months[$i] . '</option>';
                }
                else
                {
                    
    $html .= '<option value="' . ($i 1) . '">' $months[$i] . '</option>';
                }
            }
            
    $html .= '</select>&nbsp;/&nbsp;';
            
    $html .= '<input type="textbox" size="5" maxlength="4" name="field' $field->id '[]" value="' $year '" class="inputbox validate-custom-date' $class '" /> ' JText::_('YYYY');
            
    $html .= '<span id="errfield'.$field->id.'msg" style="display:none;">&nbsp;</span>';
            
    $html .= '</div>';
            
            return 
    $html;
        }
    }
    The web in one word.

  2. #2
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Not going to put it directly into the code (it looks...needlessly complex?)

    PHP Code:

    $days_array 
    = array();
    for (
    $days=1$days<32$days++)
      
    array_push($days_array$days);

    $years_array = array();
    for (
    $years 2009$years 1900$years--)
       
    array_push($years_array$years); 
    Once you got the array, just plug it back into the code within the <select> </select> html tag.

  3. #3
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Tyred to put your code into my site but did not work, looks a lot harder then i thought
    The web in one word.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Did you want Mon, Tue, Wed, Thur, Fri, Sat, Sun? Or 1, 2 , 3 ... 7.

    Anyways, heres the code:
    PHP Code:
    <?php
    $days 
    = array ("Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday");
    $years range(date("Y")-100date("Y"));

    function 
    createSelect($array$select_name){
      
    $return = array();
      
    $return[] = '<select name="'.$select_name.'">' "\n ";
      foreach(
    $array as $drop){
        
    $return[] = '<option value="'.$drop.'">'.$drop.'</option>' " \n ";
      }
      
    $return[] = '</select>' "\n ";
      return 
    implode(""$return);
    }

    echo 
    createSelect($days"days");
    echo 
    createSelect($years"year");
    ?>
    The arrays in the beginnings are just examples, its the function createSelect() you need.

    Or, I made this code a while ago. It makes select menus except gives more options then the one above:

    PHP Code:
    <?php
    $selects 
    = array('Dogs''Cats''Tigers''Hippopotamus');
    function 
    select($array$name$limit 5$etc true$shorten_val false){ //select( array_name , select_name, [limit], [display: ... after cutted])
      
    $select "<select name='{$name}'>\n";
      foreach(
    $array as $value){
        
    $display_val strlen($value) > $limit substr($value0$limit).($etc "..." "") : $value;
        if(
    $shorten_val){ $value $display_val; }
        
    $select .= "<option name='{$value}'>{$display_val}</option>\n";
      }
      
    $select .= "</select>\n";
      return 
    $select;
    }
    echo 
    select($selects'type'5truetrue);
    ?>
    Last edited by Nile; 03-18-2009 at 02:27 AM.
    Jeremy | jfein.net

  5. #5
    Join Date
    Jun 2009
    Location
    San Diego
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looking for Something Similar w/Date - Please Help

    Hello

    Just learning php - so looking for a little help.

    I would like to create a dynamically generated date drop-down with the Day, Month and Date for 10 days out in the future. For the current day it would read "Today" - so like this:

    Today
    Saturday, June 20
    Sunday, June 21
    Monday, June 22
    etc.

    I also have another drop-down in place on my project for different time selections (every 15 minutes beginning at 5:00AM and ending at 10:00PM), but that drop-down is static. I figured since it is the same all the time, I could go static - unless there is a better way to go(?)

    My next learning step will be how to capture those variables and carry throughout session and have appear on things like order email confirmation, etc.

    Hope someone can help.
    Last edited by scottdev; 06-22-2009 at 06:25 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •