想不明白的:php格式化時間問題

來源:互聯網
上載者:User

日期在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位的情況下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.