[Codeigniter-users] 日付ヘルパーについて

Back to archive index

Kenichi Ando neo.k****@gmail*****
2007年 12月 15日 (土) 22:09:49 JST


bossatamaです。

日付ヘルパーにおいて、
/helpers/date_helper.html

関数名:
unix_to_human()/human_to_unix()

サンプル:
$now = time();
echo unix_to_human($now); // U.S. time, no seconds
echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds

日付ヘルパー関数ソース
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
{
	$r  = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
		
	if ($fmt == 'us')
	{
		$r .= date('h', $time).':'.date('i', $time);
	}
	else
	{
		$r .= date('H', $time).':'.date('i', $time);
	}
	
	if ($seconds)
	{
		$r .= ':'.date('s', $time);
	}
	
	if ($fmt == 'us')
	{
		$r .= ' '.date('A', $time);
	}
		
	return $r;
}

function human_to_unix($datestr = '')
{
	if ($datestr == '')
	{
		return FALSE;
	}
	
	$datestr = trim($datestr);
	$datestr = preg_replace("/\040+/", "\040", $datestr);

	if ( ! ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\040[0-9]{1,2}:[0-9]{1,2}.*$",
$datestr))
	{
		return FALSE;
	}

	$split = preg_split("/\040/", $datestr);

	$ex = explode("-", $split['0']);
	
	$year  = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
	$month = (strlen($ex['1']) == 1) ? '0'.$ex['1']  : $ex['1'];
	$day   = (strlen($ex['2']) == 1) ? '0'.$ex['2']  : $ex['2'];

	$ex = explode(":", $split['1']);
	
	$hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
	$min  = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];

	if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
	{
		$sec  = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
	}
	else
	{
		// Unless specified, seconds get set to zero.
		$sec = '00';
	}
	
	if (isset($split['2']))
	{
		$ampm = strtolower($split['2']);
		
		if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
			$hour = $hour + 12;
			
		if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
			$hour =  '00';
			
		if (strlen($hour) == 1)
			$hour = '0'.$hour;
	}
			
	return mktime($hour, $min, $sec, $month, $day, $year);
}

日本のスタンダード書式を決めて 例:
Y年m月d日 H:i:s
がjaで帰ってくると良くないですか?その逆も。

これは実現可能な日本語版仕様だと思います。如何でしょうか。




Codeigniter-users メーリングリストの案内
Back to archive index