日本語対応のFormHelper::datetime()

使い方

<?php
// []年[]月[]日 []時[]分
$jpForm->datetime('created');
// []年[]月[]日
$jpForm->datetime('created','YMD',null);
//  []時[]分
$jpForm->datetime('created',null);
// []年度[]月
$jpForm->datetime('created','YM',null,null,array(
	'separator' => array(
		'year' => '年度',
	),
));
?>

ソースコード

<?php

class JpFormHelper extends FormHelper{
	function beforeRender(){
		$this->options['month'] = array_combine(range(1,12),range(1,12));
	}
	
	function dateTime($fieldName, $dateFormat='YMD', $timeFormat = 24, $selected = null, $attributes = array()) {
		if(empty($attributes)){
			$attributes['separator'] = array();
		}
		$attributes += array('empty' => false);
		
		$separator = '-';
		if(isset($attributes['separator'])){
			$separator = $attributes['separator'];
			$attributes['separator'] = '-';
		}
		
		if(!isset($attributes['monthNames'])){
			$attributes['monthNames'] = false;
		}
		$date = parent::dateTime($fieldName,$dateFormat,       null,$selected,$attributes);
		$time = parent::dateTime($fieldName,       null,$timeFormat,$selected,$attributes);
		
		if(is_array($separator)){
			$defaults = array(
				'year'   => '',
				'month'  => '',
				'day'    => '',
				'hour'   => '',
				'minute' => '',
				'second' => ''
			);
			
			$separator = array_merge($defaults,$separator);
			$format = array();
			if($dateFormat != 'NONE' && !empty($dateFormat)){
				foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
					switch ($char) {
						case 'Y':
							$format[] = $separator['year'];
						break;
						case 'M':
							$format[] = $separator['month'];
						break;
						case 'D':
							$format[] = $separator['day'];
						break;
					}
				}
			}
			preg_match_all('|<select.*?>.+?</select>|ms',$date,$matches);
			
			$date = "";
			for($i = 0; $i < count($matches[0]); $i++){
				$date .= $matches[0][$i] . $format[$i];
			}
			
			
			$format = array();
			if($timeFormat != 'NONE' && !empty($timeFormat)){
				switch($timeFormat){
					case 12:
						$format[] = $separator['hour'];
						$format[] = $separator['minute'];
						$format[] = '';
					case 24:
						$format[] = $separator['hour'];
						$format[] = $separator['minute'];
						break;
					default:
						break;
				}
			}
			
			preg_match_all('|<select.*?>.+?</select>|ms',$time,$matches);
			
			$time = "";
			for($i = 0; $i < count($matches[0]); $i++){
				$time .= $matches[0][$i] . $format[$i];
			}
			
		}elseif(is_string($separator)){
			$date = str_replace('</select>-','</select>'.$separator,$date);
		}
		return $date.$time;
	}
}

?>