/*
名稱: 雙向轉換西元日期 - 民國日期
用法: ch_date(來源日期(民國/西元皆可), 來源日期(年月日)分隔符號[選填], 輸出日期(年月日)分隔符號[選填]);
範例: echo ch_date(2006/12/31,'/','-'); // output: 95-12-31
備註: 直接呼叫,將輸出目前民國年份
*/
function ch_date($date="", $from="", $to="") {
if (!$date) $date = date('Ymd');
$m = substr($date,-4,2);
$d = substr($date,-2,2);
if ($from) {
if (strlen(@ereg_replace($from,'',$date))>7) {
$mode = 1;
} else $mode = 2;
} else {
if (strlen($date)>7) {
$mode = 1;
} else $mode = 2;
}
switch ($mode) {
case "1":
if ($from == '') {
$y = substr($date,0,4)-1911;
} else {
$tok = explode($from,$date);
$y = $tok[0]-1911;
$m = $tok[1];
$d = $tok[2];
}
break;
case "2":
if ($from == '') {
if (strlen($date)==7) {
$y = substr($date,0,3)+1911;
} else $y = substr($date,0,2)+1911;
} else {
$tok = explode($from,$date);
$y = $tok[0]+1911;
$m = $tok[1];
$d = $tok[2];
}
break;
}
$date = $y.$to.$m.$to.$d;
return $date;
}
名稱: 雙向轉換西元日期 - 民國日期
用法: ch_date(來源日期(民國/西元皆可), 來源日期(年月日)分隔符號[選填], 輸出日期(年月日)分隔符號[選填]);
範例: echo ch_date(2006/12/31,'/','-'); // output: 95-12-31
備註: 直接呼叫,將輸出目前民國年份
*/
function ch_date($date="", $from="", $to="") {
if (!$date) $date = date('Ymd');
$m = substr($date,-4,2);
$d = substr($date,-2,2);
if ($from) {
if (strlen(@ereg_replace($from,'',$date))>7) {
$mode = 1;
} else $mode = 2;
} else {
if (strlen($date)>7) {
$mode = 1;
} else $mode = 2;
}
switch ($mode) {
case "1":
if ($from == '') {
$y = substr($date,0,4)-1911;
} else {
$tok = explode($from,$date);
$y = $tok[0]-1911;
$m = $tok[1];
$d = $tok[2];
}
break;
case "2":
if ($from == '') {
if (strlen($date)==7) {
$y = substr($date,0,3)+1911;
} else $y = substr($date,0,2)+1911;
} else {
$tok = explode($from,$date);
$y = $tok[0]+1911;
$m = $tok[1];
$d = $tok[2];
}
break;
}
$date = $y.$to.$m.$to.$d;
return $date;
}