Location: PHPKode > scripts > DatePicker > datepicker/date_picker.php
<?php
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 Ezactive.com                                      |
// +----------------------------------------------------------------------+
// | Authors: Evgeny Leontiev <hide@address.com>                         |
// +----------------------------------------------------------------------+
//
// $Id: date_picker.php,v 1.0 2003-08-08 19:03:37+03 u1 Exp u1 $

/**
 * DatePicker is a class to generate "date_picker" easily.
 * It uses js to check and generate appropriate number of days for selected month,
 * considering for leap year.
 * It also remembers its values after "wrong submit", it means when user submit a form
 * but something is wrong, the values of "date picker" will be the same as before submit.
 * Ability to create your own, custom SELECT tag.
 *
 * After submitting a form date will be in POST or GET array
 * [date] => Array
 *   (
 *      [day] => 10
 *      [month] => 02
 *      [year] => 2003
 *   )
 * 
 *
 * @version 1.0
 * @since 08/08/2003
 * @author Evgeny Leontiev <hide@address.com>
 * @access public
 */
 
class DatePicker 
{	
	var $lang = 'en';					// month will be generated in this lang
	var $form_name = 'my_form';			// form name where select with date will be
	var $form_action;	
	
	var $select_name = 'date';			// name for SELECT tag
	
	var $day_name = 'day';				// form select name
	var $day_width = '45';				// form select width
	var $day_range = array();
	
	var $month_name = 'month';			// form select name
	var $month_width = '110';			// form select width
	var $month_range = array();
	var $month_format = '%B';			// format for month value 
										// %B - full month name
										// %b - abbreviated month name 
										// %m - month as a decimal number (range 01 to 12) 
 
	var $year_name = 'year';			// form select name
	var $year_width = '60';				// form select width
	var $year_range = array();
	
	var $custom_name = 'custom';		// form select name
	var $custom_width = '';			    // form select width
	var $custom_range = array();
	var $cur_custom = '';
	
	var $js_writed = false;
	var $css_class = '';
	var $error_pref	= '<font color="#FF0000">DATEPICKER</font>'; 	// error prefix
	

	
    /**
     * Class constructor
     *
     * @access   public
     */
	function DatePicker() {
		$this->setFormMethod($_POST); 	// default form method, to change call - setFormMethod($_GET);
		$this->setDate(); 				// now
		setlocale(LC_TIME, $this->lang); 
	}

	
   /**
	* FUNCTION: setFormName -- set form name
	*
    * @param    string    $name    form name where the "date_picker" will be   
	*
	* @return   void
	* @access   public    
	*/	
	function setFormName($name) {
		$this->form_name = $name; 
	}	
	
	
   /**
	* FUNCTION: setFormMethod -- set form method
	*
    * @param    string    $method    form method where the "date_picker" will be   
	*
	* @return   void
	* @access   public    
	*/	
	function setFormMethod($method) {
		$this->form_action =& $method;
	}
	
	
   /**
	* FUNCTION: setSelectName -- set name for SELECT tag
	*
    * @param    string    $name    form method where the "date_picker" will be   
	*
	* @return   void
	* @access   public    
	*/	
	function setSelectName($name) {
		$this->select_name = $name;
	}	

	
   /**
	* FUNCTION: setLang -- set lang
	*
    * @param    string    $lang    for details see www.php.net/manual/en/function.setlocale.php
	*
	* @return   void
	* @access   public    
	*/	
	function setLang($lang) {
		setlocale(LC_TIME, $lang); 
	}
	
	
   /**
	* FUNCTION: setDate -- set defaultd date for "date picker"
	*
	* Set date which appear on "date picker", default - current date 
	* Can be used when you edit some record, and it has some date
	*
    * @param    int timestamp    $timestamp    UNIX timestamp   
	*
	* @return   void
	* @access   public    
	*/	
	function setDate($timestamp = false) {
		
		if(empty($timestamp)) { 
			$timestamp = time();
		} 
		$this->cur_day = date('d', $timestamp);
		$this->cur_month = date('m', $timestamp);
		$this->cur_year = date('Y', $timestamp);
	}
	
	
   /**
	* FUNCTION: day -- generate select for days.
	*
	* @param    boolean   $empty_tag    (optional) show or not empty OPTION tag
	* @param    string    $empty_value  (optional) value for empty OPTION tag
	* @param    string    $empty_option (optional) option(caption) for empty OPTION tag
	*
	* @return   string    generated SELECT tag
	* @access   public    
	*/
	function day($empty_tag = false, $empty_value = '', $empty_option = '') {
		
		if(!$this->day_range) { 
			$this->setDayRange(1, 31); 
		}
		
		$select = '';
		$select .= '<select name="'.$this->select_name.'['.$this->day_name.']'.'" class="'.$this->css_class.'" style="width: '.$this->day_width.'px">'."\n";
		$select .= $this->_emptyOtionTag($empty_tag, $empty_value, $empty_option);
		$select .= $this->_optionTag('day_name', 'day_range', $this->cur_day, $empty_tag);
		$select .= '</select>'."\n";
		return $select;
	}
	
	
   /**
	* FUNCTION: setDayRange -- generate day range.
	*
	* Call it if you want special day range, by default - all days, use numbers for param 1,5,6 
	* if one param, then it should be an array, setDayRange(array(1,5,9, ...)); 
	* if two param, range will be from $start till $end, setDayRange(5, 11);
	*
	* @param    mixed    $start    'array' with values(1,2, ...) or 'int' start point 
	* @param    int      $end      (optional) end point
	*
	* @return   void
	* @access   public    
	*/
	function setDayRange($start, $end = false) {
	
		if($end) {
			for ($i = $start; $i <= $end; $i++) { 
				if($i<10) { $value = 0 . $i; }
				else 	  { $value = $i; }
				$this->day_range[$value] = $i;
			}
		} else {
			if(!is_array($start)) {
				$this->_error("If setDayRange has one param, it should be an array");
			}
			
			foreach($start as $v) {
				if($v<10) { $value = 0 . $v; }
				else 	  { $value = $v; }
				$this->day_range[$value] = $v;
			}
		}
	}
	
	
	
   /**
	* FUNCTION: month -- generate select for month.
	*
	* @param    boolean   $empty_tag    (optional) show or not empty OPTION tag
	* @param    string    $empty_value  (optional) value for empty OPTION tag
	* @param    string    $empty_option (optional) option(caption) for empty OPTION tag
	*
	* @return   string    generated SELECT tag
	* @access   public    
	*/
	function month($empty_tag = false, $empty_value = false, $empty_option = false) {
		
		if(!$this->month_range) { 
			$this->setMonthRange(1, 12); 
		}
		
		$s_js = ''; $e_js = '';
		if($this->js_writed) {
			$s_js = 'onchange="dayToMonth(\''.$this->select_name.'\')"';
			$e_js = '<script> dayToMonth(\''.$this->select_name.'\'); </script>'."\n";
		}
		
		$select = '';
		$select .= '<select name="'.$this->select_name.'['.$this->month_name.']" class="'.$this->css_class.'" style="width: '.$this->month_width.'px" '.$s_js.'>'."\n";
		$select .= $this->_emptyOtionTag($empty_tag, $empty_value, $empty_option);
		$select .= $this->_optionTag('month_name', 'month_range', $this->cur_month, $empty_tag);
		$select .= '</select>'."\n";
		$select .= $e_js;
		return $select;
	}
	
	
   /**
	* FUNCTION: setMonthRange -- generate month range.
	*
	* Call it if you want special month range, by default - all month, use numbers for param 1,5,6 
	* if one param, then it should be an array, setMonthRange(array(1,5,9, ...)); 
	* if two param, range will be from $start till $end, setMonthRange(5, 11);
	*
	* @param    mixed    $start    'array' with values(1,2, ...) or 'int' start point 
	* @param    int      $end      (optional) end point
	*
	* @return   void
	* @access   public    
	*/
	function setMonthRange($start, $end = false) {
		
		$j = 1; // pogreshnost
		if($end) {
			for ($i = $start; $i <= $end; $i++) { 
				if($i<10) { $value = 0 . $i; }
				else      { $value = $i; }
				$this->month_range[$value] = strftime($this->month_format, mktime(0,0,0,$i+$j,0,0));
			} 
		} else {
			if(!is_array($start)) {
				$this->_error("If setMonthRange has one param, it should be an array");
			}
			
			foreach($start as $v) {
				if($v<10) { $value = 0 . ($v); }
				else      { $value = $v; }
				$this->month_range[$value] = strftime($this->month_format, mktime(0,0,0,$v+$j,0,0));
			}
		}
	}
	
   
   /**
	* FUNCTION: year -- generate select for year.
	*
	* @param    boolean   $empty_tag    (optional) show or not empty OPTION tag
	* @param    string    $empty_value  (optional) value for empty OPTION tag
	* @param    string    $empty_option (optional) option(caption) for empty OPTION tag
	*
	* @return   string    generated SELECT tag
	* @access   public    
	*/
	function year($empty_tag = false, $empty_value = false, $empty_option = false) {
		
		if(!$this->year_range) { // ten years from now till -10
			$this->setYearRange(date('Y'), date('Y')-10);
		}
		
		$s_js = ''; $e_js = '';
		if($this->js_writed) {
			$s_js = 'onchange="dayToYear(\''.$this->select_name.'\')"';
			$e_js = '<script> dayToYear(\''.$this->select_name.'\'); </script>'."\n";
		}
		
		$select = '';
		$select .= '<select name="'.$this->select_name.'['.$this->year_name.']" class="'.$this->css_class.'" style="width: '.$this->year_width.'px" '.$s_js.'>'."\n";
		$select .= $this->_emptyOtionTag($empty_tag, $empty_value, $empty_option);
		$select .= $this->_optionTag('year_name', 'year_range', $this->cur_year, $empty_tag);
		$select .= '</select>'."\n";
		$select .= $e_js;
		return $select;
	}
	
	
   /**
	* FUNCTION: setYearRange -- generate year range.
	*
	* Call it if you want special year range, by default - 10 years from current year
	* if one param, then it should be an array, setYearRange(array(2000, 2010, ...)); 
	* if two param, range will be from $start till $end, setYearRange(1989, 2003);
	*
	* @param    mixed     $start    'array' with values or 'int' start point 
	* @param    string    $end      (optional) end point
	*
	* @return   void
	* @access   public    
	*/
	function setYearRange($start, $end = false) {
	
		if($end) {
			if($start > $end) {
				$range =  $start - $end;
				for ($i = 0; $i <= $range; $i++) { 
					$this->year_range[$start-$i] = $start-$i;
				}
			} else {
				$range =  $end - $start;
				for ($i = 0; $i <= $range; $i++) { 
					$this->year_range[$start+$i] = $start+$i;
				}
			}
		} else {
			if(!is_array($start)) {
				$this->_error("If setYearRange has one param, it should be an array");
			}
			
			foreach($start as $v) {
				$this->year_range[$v] = $v;
			}
		}
	}
	

   /**
	* FUNCTION: js -- generate JavaScript to manage day values
	*
	* This js checks for selected month and year and prepare appropriate day values to select.
	* Can check for: number of days in month (30 or 31 or 28) considering leap year 
	* for example when february is selected, day SELECT has only 28 day.
	*
	* @return   string    javascript
	* @access   public    
	*/
	function js() {
		ob_start();
		?>
		<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
		<!--
		month_numbDay=new Array();
		month_numbDay['01']=31;
		month_numbDay['02']=28;
		month_numbDay['03']=31;
		month_numbDay['04']=30;
		month_numbDay['05']=31;
		month_numbDay['06']=30;
		month_numbDay['07']=31;
		month_numbDay['08']=31;
		month_numbDay['09']=30;
		month_numbDay['10']=31;
		month_numbDay['11']=30;
		month_numbDay['12']=31;
		setDate=new Date();
		currentDay=setDate.getDate();
		currentMonth=setDate.getMonth();
		currentYear=setDate.getYear();
		if(((currentYear-2000)/4)==parseInt((currentYear-2000)/4)){
		    month_numbDay['02']=29;
		}
		
		function dayToMonth(name){ //forms[upload].elements[date].
		    flag1=true;
		    flag2=true;
		    day = eval ('document.forms["<?php echo $this->form_name; ?>"].elements["'+name+'[<?php echo $this->day_name; ?>]"]');
		    month = eval ('document.forms["<?php echo $this->form_name; ?>"].elements["'+name+'[<?php echo $this->month_name; ?>]"]');
		    while(flag1){
		        if(day.options[day.options.length-1].value<month_numbDay[month.options[month.selectedIndex].value]){
		            op=new Option();
		            op.text=parseInt(day.options[day.options.length-1].value)+1;
		            op.value=op.text;
		            day.options[day.options.length]=op;
		        } else{
		            flag1=false;
		        }
		    }
		    while(flag2){
		        if(day.options[day.options.length-1].value>month_numbDay[month.options[month.selectedIndex].value]){
		            if (day.selectedIndex==day.options.length-1){
		                day.selectedIndex=day.options.length-2;
		            }
		            day.options[day.options.length-1]=null;
		        } else{
		            flag2=false;
		        }
		    }
		}
		
		
		function dayToYear(name){
		    year = eval ('document.forms["<?php echo $this->form_name; ?>"].elements["'+name+'[<?php echo $this->year_name; ?>]"]');
			if( ((year.value-2000)/4)==parseInt((currentYear-2000)/4) ){
			    month_numbDay['02']=29;
			} else {
				month_numbDay['02']=28;
			}
			dayToMonth(name);
		}
		
		//dayToYear(name);
		
		//-->
		</SCRIPT>
	
		<?php
		$a = ob_get_contents();
		ob_end_clean();
		$this->js_writed = true;
		return $a;
	}
	

   /**
	* FUNCTION: setCustomName -- set name for SELECT tag.
	*
	* @param    string   $name    range_name
	*
	* @return   void
	* @access   public    
	*/
	function setCustomName($name) { 
		$this->custom_name = $name;
	}

	
   /**
	* FUNCTION: setCustomRange -- generate custom range.
	*
	* Call it if you want any special range and can't create it by setMonthRange for example
	* For $range_name use 'day_range', 'month_range', 'year_range', by default - 'custom_range'. 
	* For $array use array with key and values like this array('01' => 'January', '02' => 'February', ...);
	*
	* @param    array    $array         array with key and values 
	* @param    string   $range_name    (optional)range_name
	*
	* @return   void
	* @access   public    
	*/
	function setCustomRange($array, $range_name = 'custom_range') { 
		$this->$range_name = $array;
	}
	
	
   /**
	* FUNCTION: custom -- generate custom SELECT tag.
	*
	* @param    boolean   $empty_tag    (optional) show or not empty OPTION tag
	* @param    string    $empty_value  (optional) value for empty OPTION tag
	* @param    string    $empty_option (optional) option(caption) for empty OPTION tag
	*
	* @return   string    generated SELECT tag
	* @access   public    
	*/
	function custom($empty_tag = false, $empty_value = false, $empty_option = false) {
		
		$select = '';
		$select .= '<select name="'.$this->select_name.'['.$this->custom_name.']" class="'.$this->css_class.'" style="width: '.$this->custom_width.'px">'."\n";
		$select .= $this->_emptyOtionTag($empty_tag, $empty_value, $empty_option);
		$select .= $this->_optionTag('custom_name', 'custom_range', $this->cur_custom, $empty_tag);
		$select .= '</select>'."\n";
		return $select;
	}
	
	
   /**
	* FUNCTION: sqlDate -- return formated date value.
	*
	* Return formated date value at format (YYYYMMDDHHMMSS) to insert to database
	* which suits for DATETIME, DATE, and TIMESTAMP types
	* call it like this - DatePicker::sqlDate();
	*
	* @param    string    $name     (optional) SELECT tag name, default - date
	* @param    string    $method   (optional) form method, default - $_POST
	* @param    string    $time     (optional) time value, default current time 'hhmmss'
	*
	* @return   string    date in format (YYYYMMDDHHMMSS)
	* @access   public    
	*/
	function sqlDate($name = 'date', $method = false, $time = false) {
	
		$method = (@$method) ? $method : $_POST;
		$time = (@$time) ? $time : date("His");
		
		$str =  $method[$name]['year'].$method[$name]['month'].$method[$name]['day'].$time;
		return $str;	
	}
	
	/*-----------------------------------------------------------------------------\
    |                           private functions                                  |
    \-----------------------------------------------------------------------------*/
	
   /**#@+5          
	* @access private
	*/
	function _optionTag($name, $range, $value, $empty_tag) {
		
		@$select_name = $this->form_action[$this->select_name][$this->$name]; 
		
		if(!isset($select_name)) {
			if($empty_tag) {
				$current = 'dsrwebhgf'; // 
			} else {
				$current = $value;
			}
		} else { 
			$current = $select_name;
		}
		
		$tag = '';
		foreach($this->$range as $k => $v) {
			$selected = ($current == $k) ? ' selected' : '';
			$tag .= '<option value='.$k.$selected.'>'.$v.'</option>'."\n";
		}
		
		$this->$range = '';
		return $tag;
	}
	
	
	function _emptyOtionTag($empty_tag, $empty_value, $empty_option) {
		if($empty_tag) {
			return '<option value='.$empty_value.'>'.$empty_option.'</option>'."\n";
		}
	}

} // <-- class end
?>
Return current item: DatePicker