日期在SQL Server 2000裡是smalldatetime,現在要把裡面的資料倒到MySQL裡:timestamp,所用的工具函數如下:
function formatStringDate($strDate){ $conver=explode(" ",$strDate); $currTime=(strlen($conver[3])<4)?$conver[4]:$conver[3]; switch($conver[1]){ case "一月": $month="01"; break; case "二月": $month="02"; break; case "三月": $month="03"; break; case "四月": $month="04"; break; case "五月": $month="05"; break; case "六月": $month="06"; break; case "七月": $month="07"; break; case "八月": $month="08"; break; case "九月": $month="09"; break; case "十月": $month="10"; break; case "十一月": $month="11"; break; case "十二月": $month="12"; break; } $formatdate=$conver[0].'-'.$month.'-'.$conver[2]." ".$currTime; return $formatdate; }
把上面轉換後的格式轉換成MySQL:timestamp格式日期
function getTimeStamp($strtime){$array = explode("-",$strtime);$stryear = $array[0];$strmonth = $array[1];$array = explode(":",$array[2]);$strminute = $array[1];$strsecond = $array[2];$array= explode(" ",$array[0]);$strday = $array[0];$strhour = $array[1];$timestamp = mktime($strhour,$strminute,$strsecond,$strmonth,$strday,$stryear);return $timestamp; }
問題:
在formatStringDate函數中不加:
$currTime=(strlen($conver[3])<4)?$conver[4]:$conver[3];
當日期中的時間是3位元字是,時間部分會丟失,4位的情況下不會發生丟失情況,樣本:
2005 四月 10 0:58
formatStringDate轉換後:
2005-04-10
時間部分丟失了!這時再執行getTimeStamp函數會提示:
Warning: mktime() expects parameter 1 to be long
在formatStringDate函數中加上:
$currTime=(strlen($conver[3])<4)?$conver[4]:$conver[3];
丟失的時間就出現來.哪為什麼會是$conver[4]呢?為什麼count($conver)會得到5呢?在時間是3位的情況下