Log in

View Full Version : date drop down box



queerfm
03-17-2009, 03:01 AM
Hi i am trying to get this code to make the day and the year a drop down box just like the month



<?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;
}
}

CrazyChop
03-17-2009, 05:13 PM
Not going to put it directly into the code (it looks...needlessly complex?)




$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.

queerfm
03-18-2009, 01:42 AM
Tyred to put your code into my site but did not work, looks a lot harder then i thought

Nile
03-18-2009, 02:07 AM
Did you want Mon, Tue, Wed, Thur, Fri, Sat, Sun? Or 1, 2 , 3 ... 7.

Anyways, heres the code:


<?php
$days = array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$years = range(date("Y")-100, date("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
$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($value, 0, $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', 5, true, true);
?>

scottdev
06-20-2009, 02:39 PM
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.