Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Form Class

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I am creating a class of private functions to use for generating date/time selectors on a form but I am having difficulty making it select nothing at all! That is, it is set up to pre-populate the selectors from the Unix timestamp field of the database and there is no problem there but if the database cell is empty, it is still populating it with January 01, 1970. I understand why it is doing it but am not sure how to make it NOT do it! I need it to either submit zeros or, better yet, submit nothing at all.

Here is one of the private classes but the rest are similar:

Code:
public function createDaySelect() {
	//create the day select
	$html = "<select name=\"day".$this->selectorid."\" id=\"day".$this->selectorid."\">\n";
	$html .= "<option value=\"00\">Select Day</option>\n";
	for ($i = 1; $i <= $this->numDays; $i++) {
		$html .= "<option value=\"".sprintf("%02d", $i)."\"";
		$html .= ($this->day == $i) ? " SELECTED>" : ">";
		$html .= sprintf("%02d", $i)."</option>\n";
	}
	$html .= "</select>\n";
	return $html;
}

with an empty database date field, it is showing:

Code:
<select name="day1" id="day1">
<option value="00">Select Day</option>
<option value="01" SELECTED>01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option> . . .
 
I'd start like this.

1. In your database insert code make sure you are inserting NULL to the unixtimestamp field if the information is not present. Double check that the database is storing a NULL rather than a default value of 0.
2. in your php script, test for null
Code:
$dbDate = !isNull($dbTimeStamp) ? date('Y-m-d', $dbTimeStamp'):" - - ";
3. in your datepicker generator do something like this
Code:
function generateYearPicker($name, $min, $max, $selected=NULL){
	$template = '<select name="%s" class="yearPicker">%s</select>';
	$optionsTemplate = '<option value="%s" %s>%s</option>';
	$options = array();
	for($i=$min; $i<=$max; $i++):
		if($i == $min):
			$_selected = isNull($selected) ? 'selected="selected"' : '';
			$options[] = sprintf($optionsTemplate,'',$_selected, '&nbsp;');
		endif;
		$_selected = $selected == $i ? 'selected="selected"' : '';
		$options[] = sprintf($optionsTemplate,$i,$_selected, $i);
	endfor;
	return sprintf($template, $name, implode("\n\t", $options));
}

remember that you will also need to attach some javascript to each triptych of pickers to change the available date ranges when a value changes. here is some code I just wrote to do it (load it as an html page and you will see what happens)

Code:
<!DOCTYPE HTML>
	<html>
		<head>
			<script src="[URL unfurl="true"]http://code.jquery.com/jquery-1.10.1.min.js"></script>[/URL]
			<script type="text/javascript">
function bindDatePickers(y,m,d){    
	$(y).on('change', function(e){
        check(y,m,d);
        if($(y).val() == '') return;
      	if($(m).val() == 2){
			/* deal with leap years */
			var curYear = $(this).val();
			if(curYear % 400 == 0){
				adjustDays($(d), 29);
			}
			else if(curYear % 100 == 0){
				adjustDays($(d), 28);
			}
			else if(curYear % 4 == 0){
				adjustDays($(d), 29);
			}
			else{
				adjustDays($(d), 28);
			}
		}
	});
	
	$(m).on('change', function(e){ 
        check(y,m,d);
        var v = parseInt( $(this).val() );
        if(isNaN(v)) return;
        if(v == 2){
                var curYear = $(y).val();
                if(curYear % 400 == 0){
					adjustDays($(d), 29);
				}
				else if(curYear % 100 == 0){
					adjustDays($(d), 28);
				}
				else if(curYear % 4 == 0){
					adjustDays($(d), 29);
				}
				else{
					adjustDays($(d), 28);
				}
        }
        else if(v==9 || v == 4 || v == 6 || v == 11){
            adjustDays($(d), 30);
        }
        else{
            adjustDays($(d), 31);
		}
	});
    $(d).on('change', function(e){ 
        check(y,m,d);
    });
}
function check(y,m,d){
    	var i = 0;
        var a = [y,m,d];
		$.each(a, function(j, v){
			if(v.val() == '') i++;
		});
		
        var bg = (i > 0 && i < 3) ? 'red' : 'white';
		$.each(a, function(j,v){
				v.css('background-color', bg);
		});
}

function adjustDays(d, num){
    var x = 0;	
	$(d).find('option').each(function(i, e){
		if($(this).val() > num){
			$(this).remove();
		}
		x = i;
	});
	
	for(x; x<num;x++){
		var o = $('<option>');
		o.val(x+1);
		o.text(x+1);
		d.append(o);
	}
}

function fill(y, m, d, ysel, msel, dsel, ymin, ymax){
    ysel = typeof ysel == 'undefined' ? "" : ysel;
    msel = typeof ysel == 'undefined' ? "" : msel;
    dsel = typeof ysel == 'undefined' ? "" : dsel;
    ymin = typeof ymin == 'undefined' ? 1920 : ymin;
	ymax = typeof ymax == 'undefined' ? 2020 : ymax;
    var fo = $('<option>');
    fo.val('');
    fo.text('Select');
    
    y.append(fo.clone(true));
    for(var i = ymin; i<=ymax; i++){
        var n = $('<option>');
        n.val(i);
        n.text(i);
        y.append(n);
    }
    
    m.append(fo.clone(true));
	var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
    for(var i = 1; i<=12; i++){
        var n = $('<option>');
        n.val(i);
        n.text(months[i - 1]);
        m.append(n);
    }
    
    d.append(fo.clone(true));
    for(var i = 1; i<=31; i++){
        var n = $('<option>');
        n.val(i);
        n.text(i);
        d.append(n);
    }
    
    y.val(ysel);
    m.val(msel);
    d.val(dsel);
    
    bindDatePickers(y,m,d);
    y.trigger('change');
    m.trigger('change');
    d.trigger('change');
}

$(document).ready(function(){
	fill($('#year'), $('#month'), $('#day'));
	fill($('#year2'), $('#month2'), $('#day2'));
	
});

			</script>
		</head>
		<body>
<select name="year" id="year">
</select>

<select name="month" id="month">
</select>

<select name="day" id="day">
</select>
<hr/>

<select name="year2" id="year2">
</select>

<select name="month2" id="month2">
</select>

<select name="day2" id="day2">
</select>
		</body>
	</html>

I have also posted a jsfiddle of this so you can easily see it in action.. In the above code, the js can take the full load of the php script. you would call it like this (from php)
JavaScript:
fill($('#year'), $('#month'), $('#day'), <?php echo $year;?>,<?php echo $month;?>,<?php echo $day;?>, <?php echo $minYear;?>, <?php echo $maxYear;?> );
 
I'm back to this finally. Since I'm the only one using it I am not too concerned about how many days are on a month but I'll keep the code handy for later use.

I am still having difficulty, though, it having the selector not show anything if the field is NULL or if the time and/or date is missing. The fields, as $input, contain the Unix timestamp but sometimes it has only the date without any time and there may be rare occasions where there is time or maybe just the year without any other date. (The $SelID variable simply lets me have more than one set of selectors on the same form by giving each its own suffix for the name and id values.)

I tried this for one of the selectors:

Code:
class DateSelects {
	private $months;
	private $month;
	private $today;
	private $year;
	private $hour;
	private $minute;
	private $second;
	private $SelID;
	private $numDays;
 
	public function __construct($input, $SelID) {
		$this->months = array(1=> 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
		$this->month = date("n", $input);
		$this->day = date("d", $input);
		$this->year = date("Y", $input);
		$this->hour = date("G", $input);
		$this->minute = date("i", $input);
		$this->second = date("s", $input);
		$this->SelID = $SelID;
		$this->numDays = 31;
	}

	public function createMonthSelect() {
		//create the month select
		$html = "<select name=\"month".$this->SelID."\" id=\"month".$this->SelID."\">\n";
		$html .= "<option value=\"\"></option>\n";
		for ($i = 1; $i <= 12; $i++)	{
			$html .= "<option value=\"".sprintf("%02d", $i)."\"";
			$html .= ($this->month == $i [COLOR=red][bold]&& !is_null($input)[/bold][/color]) ? " SELECTED>" : ">";
			$html .= $this->months[$i]."</option>\n";
		}
		$html .= "</select>\n";
		return $html;
	}

// . . . . ETC.

}
 
please post the table specification from a show columns query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top